ProgrammingHardTCS NQT · Wipro

How many set bits does this loop count in 0xFFFF0000?

How many set bits does this loop count in 0xFFFF0000?
#include <stdio.h>
int main(void) {
    unsigned x = 0xFFFF0000u;
    int c = 0;
    while (x) { c += x & 1u; x >>= 1; }
    printf("%d", c);
}
  1. A. 32
  2. B. 16
  3. C. 8
  4. D. 15

Correct answer: B. 16

The upper sixteen bits are all set and the lower sixteen are clear, so the count is 16.

More C Programming questions

Exams that ask this topic