ProgrammingVery hardGATE CS · Campus placements
uint32_t x = 0u; Which statement about the test (x & (x - 1)) == 0 is correct?
- A. It is true for x = 0, so a power-of-two check must also require x != 0
- B. It is false for x = 0, because x - 1 underflows
- C. It is true only for x = 1
- 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
- uint8t x = 0x81; uint8t r = (uint8t)((x >> 1) | (x << 7)); What is r?
- What is the value of 1 << 2 + 3 in C?
- int x = 2; printf("%d", x & 1 == 1); prints what?
- uint32t x = 1u; x <<= 31; x <<= 1; What is x afterwards?
- What is the value of (uint64t)1 << 40?
- uint64t computes 3 to the power 40 modulo 1000000007 with square-and-multiply, reducing after each multiplication. What is the result?