为什么C语言中是1^-1 = -2?

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

谁能解释一下 x^-1 在下面的代码中是如何执行的。我试过了但没有运气。我了解 x^1 是如何执行的。

#include <stdio.h>
int main(void) 
{
int a=220, b=221, c=3;
printf("a^1= %d ,, a^-1= %d \n", a^1, a^-1);
printf("b^1= %d ,, b^-1= %d \n", b^1, b^-1);
printf("c^1= %d ,, c^-1= %d \n", c^1, c^-1);
return 0;
}
/* output: a^1= 221 ,, a^-1= -221
           b^1= 220 ,, b^-1= -222
           c^1= 2   ,, c^-1= -4    */

^运算符是C中的XORexclusive-or运算符。 为简单起见,考虑使用典型的 two's-complement encoding 的 8 位有符号值。 int 类型的工作方式相同。

Decimal   Binary
      1   00000001
     -1   11111111
          -------- XOR
     -2   11111110

请注意,一元运算符 - 的优先级高于 ^ 按位运算符。

Signed ints 使用Two's complement 来表示负数。

1 以 int32 表示:

0000 0000 0000 0000 0000 0000 0000 0001

-1 以 int32 表示,使用补码:

1111 1111 1111 1111 1111 1111 1111 1111

XOR计算两个结果:

1111 1111 1111 1111 1111 1111 1111 1110

对于已签名的 ints,它是 -2

对于无符号整数,这将是 2^32 - 2