[Solved] Why is 1^-1 = -2 in the C language?


Signed ints 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

XORing the two results in:

1111 1111 1111 1111 1111 1111 1111 1110

which, for signed ints, is -2.

for unsigned ints, that would be 2^32 - 2.

0

solved Why is 1^-1 = -2 in the C language?