I am reading a code where it is supposed to implement a bit vector using a byte array.
The idea is that the bitvector has the bit set if a number is present at the corresponding position.
E.g. if number 10 is present the bit 10 must be set etc. It is a classic concept and I get it, but I am not sure about the actual implementation.
The part I don’t get is:
bitvector [num / 8] |= 1 << (num % 8);
Where num is the number to set.
If num is 10 then the second byte must be used (num/8 ok so far) but 1 << (num % 8) does not set the second bit of the second byte as it should. Does it?
10 % 8 = 2, therefore1 << (10 % 8) = bit 2, or the value 4 (100 in binary)(start counting bits from the right side of the byte, starting at zero). Very simple to verify: