ProgrammingVery hardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    unsigned x = 0xDEAD;
    int c = 0;
    unsigned t = x & 0xAAAA;
    while (t) { c += t & 1u; t >>= 1; }
    printf("%d", c);
}
  1. A. 5
  2. B. 35496
  3. C. 6
  4. D. 11

Correct answer: C. 6

0xDEAD & 0xAAAA keeps only the odd-position bits, giving 0x8AA8 = 1000 1010 1010 1000, which has 6 set bits. 5 is the count for the even mask 0x5555, 11 counts all bits of 0xDEAD, and 35496 is the masked value itself, not its popcount.

More C Programming questions

Exams that ask this topic