How come performing the unary NOT (~) opertion on int x = 0 gives the value of -1 and not a saturated int value?
Printable View
How come performing the unary NOT (~) opertion on int x = 0 gives the value of -1 and not a saturated int value?
Because of two's complement representation.
(x) + (~x +1) must always equal 0.
0 + -1 + 1 = 0
Even with unsigned integers?
You're just flipping all the bits, so the result can never be saturated. In a one's complement system the result would be 0 (-0) (signed).
How were you expecting negative numbers to be represented in an int?
Yea it makes sense to me for signed integers, but for unsigned integers I am not clear why this is also the case. Guess I should brush up on two's complement.
OK I think I see that C does not allow unsigned integers of this magnitude, but the bit representation is still all ones, correct? That's all that really matters to me.
For unsigned number you should get INT_MAX.
Code:#include <stdio.h>
int main() {
unsigned int x = 0;
printf("%u\n", ~x);
}
%u, very helpful, thanks.