c - 如何在c中对多维char数组进行位运算

标签 c multidimensional-array bitwise-operators

我有以下多维字符数组。我需要首先将其中一个数组与掩码数组进行“异或”,然后将结果与另一个数组进行“与”操作。最快的实现方法是什么?

注意:字符数组的大小可达 20 KB。

unsigned char test1[2][2] = { { 'a','b' },{ 0 } };
unsigned char test2[2][2] = { { 0 },{ 'O','S' } };
unsigned char mask[2][2] = { 0 };

最佳答案

类似的事情可能会很快:

int cnt = sizeof(mask)/sizeof(unsigned char)

while(cnt>0)
{
    *((unsigned char*)test1+cnt) ^= *((unsigned char*)mask+cnt);
    *((unsigned char*)test1+cnt) &= *((unsigned char*)test2+cnt);
    cnt--;
}

只有一次通过数组的方式,并且只移动 3 个指针,这可能会被编译器很好地优化。为了最有效,我们必须做一些测试并查看汇编输出。 IT 还取决于您使用的架构(X86、ARM、AVR、PIC...)

关于c - 如何在c中对多维char数组进行位运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46092439/

相关文章:

c - 欧拉计划 #7 : is anything wrong in the code?

c++ - C++ 类中公共(public)函数的地址

c - 在 C 中声明字符串与整数的二维数组?

c - C代码的奇怪行为(fgets函数)

c - pixman 和 clutter box2d 应用程序

php - 存储多维数组中的特定值

string - VBA:字符串和数组 - 将一维数组拆分为二维数组

python - (~) 为什么我的位没有翻转?

java - Java中的按位运算子串

c# - 帮助进行位运算