ProgrammingVery hardGATE CS · Campus placements

uint32_t x = 0u; Which statement about the test (x & (x - 1)) == 0 is correct?

  1. A. It is true for x = 0, so a power-of-two check must also require x != 0
  2. B. It is false for x = 0, because x - 1 underflows
  3. C. It is true only for x = 1
  4. D. It is undefined behaviour for x = 0

Correct answer: A. It is true for x = 0, so a power-of-two check must also require x != 0

Unsigned x - 1 wraps to 4294967295 when x is 0, and 0 AND anything is 0, so the test passes. Unsigned wraparound is well defined, so the trap is logical, not undefined behaviour.

More C Programming questions

Exams that ask this topic