ProgrammingHardProduct company OA

What is printed?

What is printed?
int t = 0;
for (unsigned i = 0; i < 16; i++)
    for (unsigned x = i; x; x &= x - 1) t++;
printf("%d", t);
  1. A. 30
  2. B. 32
  3. C. 16
  4. D. 64

Correct answer: B. 32

The inner loop runs once per set bit of i (each x &= x − 1 removes one). Over 0 to 15, each of the 4 bit positions is set in exactly half of the 16 numbers: 4 × 8 = 32.

More C Programming questions

Exams that ask this topic