ProgrammingVery hardGATE CS · Campus placements

A uint32_t x = 37 is rounded up to a power of two by this code: x--; x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x++; What is x?

  1. A. 37
  2. B. 40
  3. C. 64
  4. D. 32

Correct answer: C. 64

After x-- the value is 36 = 100100. Smearing sets every bit below the top set bit, giving 63, and adding one gives 64. The x-- at the start is what makes exact powers of two map to themselves.

More C Programming questions

Exams that ask this topic