Bitwise operation in C related to position in 8-bit array

I'm currently doing bitwise operations, and I found a method in which the reference to the position in the 8-bit array is not clear to me.

Here is an example: all the numbers I have chosen at random for explanation only, the method itself is much more complicated (the array can be initialized with any size large enough):

void method(int pos, uint8_t* array_of_num) {

  int dim = 5;
  uint8_t numbers[4] = {1, 8, 32, 64}; 

  int size = 10;
  int i = 0;
  for (i=0; i < size; i++){

     array_of_num[pos + i/4] |= numbers[i%4];   // <======
  }

}  

I don’t understand what position "array_of_num [pos + i / 4]" refers to. array_of_num is an array of 8-bit uints, so it looks like this:

[0] ----> 1 (00000001); 
[1] ----> 5 (00000101); 
[2] ----> ......  

This applies to some int bits at a position in the array, but if so, then as an operand - | - takes into account only these bits, since the numbers [] are also 8 uint bits. Let me try to explain what I mean in this example, so this is:

pos = 0; i = 1
array_of_num[0 + 1/4] |= 00001000 

will not be the same if we just write:

array_of_num[0] |= 00001000
+4
1

, array_of_num (size, pos).

8- pos, 0, 3, 5 6, , 0 3.

:

array_of_num[pos] | = 1;
array_of_num[pos] | = 8;
array_of_num[pos] | = 32;
array_of_num[pos] | = 64;
array_of_num[pos+1] | = 1;
array_of_num[pos+1] | = 8;
array_of_num[pos+1] | = 32;
array_of_num[pos+1] | = 64;
array_of_num[pos+2] | = 1;
array_of_num[pos+2] | = 8;

, :

array_of_num[pos] |= (1|8|32|64);
array_of_num[pos+1] |= (1|8|32|64);
array_of_num[pos+2] |= (1|8);

, i = 0; array_of_num[0 + i/4] array_of_num[0], i=6, . . modulo numbers.

+2

Source: https://habr.com/ru/post/1598658/


All Articles