ProgrammingHardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    unsigned v = 0x01020304u, total = 0;
    for (int i = 0; i < 4; i++) {
        unsigned b = (v >> (8 * i)) & 0xFFu;
        while (b) { total += b & 1u; b >>= 1; }
    }
    printf("%u", total);
}
  1. A. 4
  2. B. 7
  3. C. 5
  4. D. 6

Correct answer: C. 5

The four bytes of 0x01020304 are 4, 3, 2 and 1, whose set-bit counts are 1, 2, 1 and 1. Their total is 5.

More C Programming questions

Exams that ask this topic