use*_*272 3 c bit-manipulation
我对按位运算所需的周期数有疑问,或者更确切地说,是XOR运算.在我的程序中,我有两个uint8_t变量的一维数组,固定大小为8.我想对两个数组进行异或,我想知道最有效的方法是什么.这是一个总结我发现的选项的代码:
int main() {
uint8_t tab[4] = {1,0,0,2};
uint8_t tab2[4] = {2,3,4,1};
/* First option */
uint8_t tab3[4] = {tab[0]^tab2[0], tab[1]^tab2[1], tab[2]^tab2[2], tab[3]^tab2[3]};
/* Second option */
uint32_t* t = tab;
uint32_t* t2 = tab2;
uint32_t t3 = *t ^ *t2;
uint8_t* tab4 = &t3;
/* Comparison */
printf("%d & %d\n", tab3[0], tab4[0]);
printf("%d & %d\n", tab3[1], tab4[1]);
printf("%d & %d\n", tab3[2], tab4[2]);
printf("%d & %d\n", tab3[3], tab4[3]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
从循环/字节的角度来看,最佳选择是什么?
| 归档时间: |
|
| 查看次数: |
398 次 |
| 最近记录: |