1. How can byte = (value & mask) / shift isolate individual bits?
mask is a pattern bit that always has 8 consecutive bits set to 1, the rest is 0 (starts with 0xff000000 , then 0x00ff0000 , etc. Therefore, when you accept the bitwise and mask and value , all bits from value will be set to 0, except those that match the byte specified by mask . They retain their value.
shift set to the corresponding value, which by dividing with shift exactly those bits that survived in the disguise fall into the rightmost bits (see the answer to question 3, how it works).
Suppose value is 0xDEADBEEF , mask has an initial value of 0xff000000 , and shift has an initial value of 256*256*256 . Then value & mask is 0xDE000000 , and the final result is 0x000000DE .
In binary format, the example will be
value = 11011110101011011011111011101111 mask = 11111111000000000000000000000000 byte & mask = 11011110000000000000000000000000 result = 00000000000000000000000001101111
2. Why if(byte & 0x80) means "If the most significant bit in a byte is not 0?"
Here, the author of the code believes that byte is an 8-bit variable. Although this is technically larger, higher bits are never used here. Therefore, when the author refers to the “high bit”, think about the 8th bit on the right (the most significant bit that would be there if the byte was actually only one byte in size).
Now notice that 0x80 is 10000000 in binary format. Therefore, when you take byte & 0x80 , all bits from byte will be set to 0, except for the "highest" (8th on the right). Thus, byte & 0x80 is zero if the most significant bit from byte is zero and greater than zero, if the “highest” bit of byte is 1.
3. Like these lines: byte *= 2; , mask /= 256; and shift /= 256; move bits and why is this operation significant?
Multiplying from 2 is equivalent to shifting the bits to the left by 1. Consider, for example, the value 9, which is 1001 in binary format. Multiplying by 2 gives 18, which is 10010 in binary format.
Similarly for dividing by 2, this shift to the right by 1. Division by 256 is equivalent to 8 divisions by 2, therefore dividing by 256 is equivalent to a shift to the right by 8 bits. These operations are used here, for example, to change the mask value from 0xff000000 to 0x00ff0000 , 0x0000ff00 and finally to 0x000000ff .
Full Feature Description
With this knowledge, we can see what the full function does. In the outer loop, it bypasses four bytes that are in value , starting with the leftmost and ending with the rightmost. It does this by masking the current byte and storing it in a byte .
The inner loop then goes through 8 bits, which are stored in the byte . It always looks at the eighth bit on the right and prints 1 or 0, respectively. Then it shifts the bits to the left, so that in the second iteration, the bit that was the seventh on the right is now the eighth on the right and will be printed, and then the next, etc., Until all 8 bits are printed on the right - the left order.
An alternative way to write this function would be
for (int i = 31; i >= 0; i--) { if (value & (1 << i)) printf("1"); else printf("0"); if (i % 8 == 0) printf(" "); }
It will just traverse all the bits from value in order from left to right. The expression value (1 << i) selects the desired bit from value , starting from the 32nd on the right (when i is 31) and ending with the 1st on the right (when i is 0).