ProgrammingVery hardProduct company OA

What is printed?

What is printed?
unsigned x = 0xF0F0;
x = x - ((x >> 1) & 0x5555);
x = (x & 0x3333) + ((x >> 2) & 0x3333);
x = (x + (x >> 4)) & 0x0F0F;
x = (x + (x >> 8)) & 0x1F;
printf("%u", x);
  1. A. 16
  2. B. 4
  3. C. 8
  4. D. 240

Correct answer: C. 8

This is the parallel (SWAR) bit count for 16 bits: it sums bits in pairs, then nibbles, then bytes, then the two bytes. 0xF0F0 has 8 bits set. The masks keep each partial sum inside its own field, so the steps never interfere.

More C Programming questions

Exams that ask this topic