python 32位负数存储格式
print(bin(-1))
print(bin((-1)&0xffffffff))
输出
-0b1
0b11111111111111111111111111111111
Process finished with exit code 0
-1 的二进制原码0000 0000 0000 0001
反码 1111 1111 1111 1110
补码 1111 1111 1111 1111
负数-1的原码是0b1,python存储直接在前面加了一个负号
要想和c++一样得到负数的补码,可以使用(-1)&0xffffffff
这样就得到了-1的补码0b11111111111111111111111111111111