ProgrammingVery hard
Which expression sets bit 31 of an unsigned 32-bit variable x with no undefined behaviour?
- A. x |= 1 << 31;
- B. x |= 1 << 32;
- C. x |= 1u << 31;
- D. x = 1 << 31 - 1;
Correct answer: C. x |= 1u << 31;
1u << 31 is an unsigned shift that stays within the type. 1 << 31 shifts into the sign bit of int, which is undefined.
More C Programming questions
- The sequence n ^ (n >> 1) for n = 0, 1, 2, 3 gives what list?
- For positive values below 2^31, which product is guaranteed to fit in a signed 64-bit integer?
- What is the multiplicative inverse of 2 modulo 7?
- In C, what is the result of 7 / -2 and 7 % -2?
- An unsigned short variable holds 65535 and is then incremented once with x++. What value does it hold?
- Which statement about left-shifting a negative signed integer in C is correct?