The AND bitwise operator can be used to mask bits off. We often use the AND bitwise operation to mask off some bits, as a 0 in a certain bit position will make the result ‘0’, while a ‘1’ at this position will preserve the value of the bit. For example the following are bit masks for 1, 2 and 3:
128 64 32 16 8 4 2 1 Decimal b7 b6 b5 b4 b3 b2 b1 b0 Val 0 0 1 1 0 1 0 1 53 Val & 0x1 0 0 0 0 0 0 0 1 1 Val & 0x2 0 0 0 0 0 0 1 0 2 Val & 0x3 0 0 0 0 0 0 0 1 1