Bitwise & can be used to check if a bit in number is set or not.
Answer: A
Correct answer: True
This is the standard bit test. Build a mask with a single 1 in the position of interest, usually as 1 << k, and AND it with the value: n & (1 << k) is non-zero when the bit is set and zero when it is clear.
Note that the non-zero result is \( 2^k \), not 1, so the expression should be tested against zero rather than compared with 1. AND is the natural operator for the job because it forces every unselected position to zero, leaving only the bit being examined.
Please log in or register to report this question.