ProgrammingVery hardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    static const unsigned char t[16] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
    unsigned x = 0xF0F0, c = 0;
    for (int i = 0; i < 4; i++) {
        c += t[x & 0xF];
        x >>= 4;
    }
    printf("%u", c);
}
  1. A. 4
  2. B. 16
  3. C. 8
  4. D. 12

Correct answer: C. 8

The table holds the popcount of each nibble. The nibbles of 0xF0F0 are F, 0, F, 0, whose table values are 4, 0, 4, 0, summing to 8.

More C Programming questions

Exams that ask this topic