What is printed by this C code: printf(“%d”, 6 & 3 == 3);?
Correct answer B. 0
Explanation
Equality operators have higher precedence than bitwise &, so the expression is 6 & (3 == 3), which is 6 & 1. Binary 110 AND 001 is 0. Someone expecting (6 & 3) == 3 would compute 2 == 3 and still get 0 by a different route, but would give 2 if they forgot the comparison. Always parenthesise bit tests, as in (x & MASK) == MASK.
Answer checked How we write and check questions
Discussion