ProgrammingVery hardProduct company OA

Assume 32-bit unsigned int. What is printed?

Assume 32-bit unsigned int. What is printed?
unsigned int x = 0x5Cu;            /* 0101 1100 */
unsigned int low = x & -x;
unsigned int y = x + low;
unsigned int z = y | (((x ^ y) / low) >> 2);
printf("%u %u %u", low, y, z);
  1. A. 4 96 111
  2. B. 4 96 99
  3. C. 4 96 96
  4. D. 2 94 95

Correct answer: B. 4 96 99

x & −x isolates the lowest set bit: 4. Adding it ripples through the lowest block of ones (111 at bits 2–4) and sets the next zero: y = 0110 0000 = 96. x ^ y = 0011 1100 marks the changed bits; dividing by low right-aligns it (1111) and >> 2 drops two, leaving 11 – the block's remaining ones moved to the bottom. z = 0110 0011 = 99: the next larger integer with the same number of 1-bits (Gosper's hack). 111 forgets the >> 2 and gains a bit.

More C Programming questions

Exams that ask this topic