Which expression tests whether bit n (counting from 0) of an unsigned integer x is set?
Correct answer C. (x >> n) & 1
Explanation
Shifting x right by n moves bit n into position 0, and & 1 keeps only that bit, so the result is 1 if it was set and 0 otherwise. The equivalent form x & (1u << n) gives a non-zero value when set. x | (1u << n) sets the bit rather than testing it, and x & n wrongly treats n as a mask.
Answer checked How we write and check questions
Discussion