ProgrammingHardProduct company OA

What is printed?

What is printed?
unsigned p = 0xB5;
p ^= p >> 4;
p ^= p >> 2;
p ^= p >> 1;
printf("%u", p & 1);
  1. A. 5
  2. B. 1
  3. C. 3
  4. D. 0

Correct answer: B. 1

Each step folds the upper half onto the lower half with XOR, so bit 0 ends up as the XOR of all eight bits, i.e. the parity. 0xB5 = 1011 0101 has five 1s, an odd number, so the result is 1. '5' is the bit count itself.

More C Programming questions

Exams that ask this topic