Search code examples
cbitwise-or

Bitwise or between 2 sets of bytes in c


Let's say we have

int *set1 = malloc(n);

int *set2 = malloc(n);

Then we fill these 2 sets with data. My goal is to perform bitwise or between set1 and set2.

I suppose I can't do something like

for (i=0; i<n; i++) {
    set1[i] = set1[i] | set2[i];
}

because n is the number of bytes and not the number of cells of the array.

Any ideas? Thanks in advance


Solution

  • set1 and set2 are pointers to integers. You try to process bytes. This is not correct.

    Try to picture the memory layout:

    malloc(1) would allocate one byte, i.e. 8 bits. int *set1 declares a pointer to an int, which typically is 4 bytes, i.e. 32 bits.

    A char typically represents 1 byte, i.e. 8 bits. So why not go with that, char *set1 and char *set2 ?