ProgrammingVery hardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdint.h>
#include <stdio.h>
int main(void) {
    uint32_t v = 0x00F0F0A5u;
    v = v - ((v >> 1) & 0x55555555u);
    v = (v & 0x33333333u) + ((v >> 2) & 0x33333333u);
    v = (v + (v >> 4)) & 0x0F0F0F0Fu;
    v = (v * 0x01010101u) >> 24;
    printf("%u", v);
}
  1. A. 12
  2. B. 10
  3. C. 8
  4. D. 14

Correct answer: A. 12

This is the SWAR popcount: each step adds neighbouring bit counts, and the multiply by 0x01010101 sums the four byte counts into the top byte. The three bytes 0xF0, 0xF0 and 0xA5 hold 4 + 4 + 4 = 12 set bits.

More C Programming questions

Exams that ask this topic