ProgrammingVery hardProduct company OA

What is printed?

What is printed?
unsigned x = 8;
if (x & (x - 1) == 0) puts("pow2");
else puts("not");
  1. A. pow2
  2. B. Nothing
  3. C. not
  4. D. Compilation error

Correct answer: C. not

== binds tighter than &, so the test is x & ((x − 1) == 0) = 8 & 0 = 0, which is false, even though 8 is a power of two. The intended test needs parentheses: (x & (x − 1)) == 0.

More C Programming questions

Exams that ask this topic