Signed int
s use Two’s complement in order to represent negative numbers.
1
represented in int32:
0000 0000 0000 0000 0000 0000 0000 0001
-1
represented in int32, using Two’s complement:
1111 1111 1111 1111 1111 1111 1111 1111
XOR
ing the two results in:
1111 1111 1111 1111 1111 1111 1111 1110
which, for signed int
s, is -2
.
for unsigned ints, that would be 2^32 - 2
.
0
solved Why is 1^-1 = -2 in the C language?