ProgrammingHardTCS NQT · Wipro

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    unsigned n = 0;
    printf("%d", (n & (n - 1)) == 0);
}
  1. A. 0
  2. B. 1
  3. C. 2
  4. D. 3

Correct answer: B. 1

n - 1 wraps to UINT_MAX, and 0 & UINT_MAX is 0, so the test is true and prints 1. Zero therefore passes a power-of-two test written this way.

More C Programming questions

Exams that ask this topic