Practice mode C Language

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.