In this code:
- bit_a = (i & 2) / 2; // Get the second bit.
it says get the second bit. 2 is 00000010. The 1 is in bit 1, not bit 2. It is bit 1 because the position of the bit is 2 to the 1st power.
The bit on the far right is bit 0. To the left of bit 0 is bit 1.
The bit is either ON (1) or OFF(0).
Now look at i & 2:
Let's look at the possible conditions
00000010
or
00000000
If you AND with 2 (00000010) you get:
00000010
00000010
.................AND
00000010
or you get
00000000
00000010
.................AND
00000000
In the first case the result of the AND is 00000010, which is 2. i & 2 becomes 2. So now you take 2 / 2 which is 1. Hence the bit is ON.
The other case is 00000000 which is 0. Then 0/2 is 0. Hence the bit is OFF.