What's the most efficient way to make bitwise operations in a C array

后端 未结 3 1945
死守一世寂寞
死守一世寂寞 2021-02-08 13:24

I have a C array like:

char byte_array[10];

And another one that acts as a mask:

char byte_mask[10];

I would

3条回答
  •  心在旅途
    2021-02-08 13:54

    \#define CHAR_ARRAY_SIZE    (10)
    \#define INT_ARRAY_SIZE     ((CHAR_ARRAY_SIZE/ (sizeof (unsigned int)) + 1)
    
    typedef union _arr_tag_ {
    
        char          byte_array [CHAR_ARRAY_SIZE];
        unsigned int  int_array [INT_ARRAY_SIZE]; 
    
    } arr_tag;
    

    Now int_array for masking. This might work for both 32bit and 64 bit processors.

    arr_tag arr_src, arr_result, arr_mask;
    
    for (int i = 0; i < INT_ARRAY_SIZE; i ++) {
        arr_result.int_array [i] = arr_src.int_array[i] & arr_mask.int_array [i];
    }
    

    Try this, code might also look clean.

提交回复
热议问题