ProgrammingHardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    unsigned x = 0;
    int c = 0;
    for (unsigned i = 1; i <= 16; i++) {
        x = i;
        while (x) { c += x & 1u; x >>= 1; }
    }
    printf("%d", c);
}
  1. A. 32
  2. B. 33
  3. C. 34
  4. D. 36

Correct answer: B. 33

The bit counts of 1 to 16 add up to 33: the numbers 1 to 15 contribute 32 and 16 contributes 1.

More C Programming questions

Exams that ask this topic