Computer Arithmetic
In C, what is the result of subtracting 1 from 0 in a 32-bit unsigned integer (unsigned int)?
- A−1
- B4294967295
- CIt stays at 0
Show answer and explanation
Answer: B. 4294967295
Unsigned integers have no negatives, and C defines their results modulo 2³² (wrapping around). 0 − 1 becomes 2³² − 1 = 4294967295, all ones in binary. That is why a loop that counts an unsigned variable down with the condition i >= 0 never ends: the condition is always true. Unsigned types cannot represent −1, and they do not saturate at 0.
- A. Unsigned integers cannot represent negative numbers.
- B. Correct. It wraps around to 2³² − 1 = 4294967295.
- C. It does not saturate at 0.