为什么〜True导致-2?

问题:为什么〜True导致-2?

在Python控制台中:

~True

给我:

-2

为什么?有人可以用二进制给我解释这个特殊情况吗?

In Python console:

~True

Gives me:

-2

Why? Can someone explain this particular case to me in binary?


回答 0

int(True)1

1 是:

00000001

并且~1是:

11111110

这是-2两个补1

1翻转所有位,在结果数上加1,然后将结果解释为幅度的二进制表示,并添加一个负号(因为数字以1开头):

11111110  00000001  00000010 
                    
       Flip       Add 1

它是2,但由于MSB为1 ,所以符号为负。


值得一提:

想一想bool,您会发现它本质上是数字-它有两个值TrueFalse,它们只是整数1和0的“自定义”版本,它们的打印方式不同。它们是整数类型的子类int

因此它们的行为与1和0完全相同,只是bool重新定义strrepr以不同的方式显示它们。

>>> type(True)
<class 'bool'>
>>> isinstance(True, int)
True

>>> True == 1
True
>>> True is 1  # they're still different objects
False

int(True) is 1.

1 is:

00000001

and ~1 is:

11111110

Which is -2 in Two’s complement1

1 Flip all the bits, add 1 to the resulting number and interpret the result as a binary representation of the magnitude and add a negative sign (since the number begins with 1):

11111110 → 00000001 → 00000010 
         ↑          ↑ 
       Flip       Add 1

Which is 2, but the sign is negative since the MSB is 1.


Worth mentioning:

Think about bool, you’ll find that it’s numeric in nature – It has two values, True and False, and they are just “customized” versions of the integers 1 and 0 that only print themselves differently. They are subclasses of the integer type int.

So they behave exactly as 1 and 0, except that bool redefines str and repr to display them differently.

>>> type(True)
<class 'bool'>
>>> isinstance(True, int)
True

>>> True == 1
True
>>> True is 1  # they're still different objects
False

回答 1

Python bool类型是的子类int(出于历史原因;布尔值仅在Python 2.3中添加)。

由于int(True)1~True~1-2

有关为什么是的子类,请参见PEP 285boolint

如果需要布尔逆,请使用not

>>> not True
False
>>> not False
True

如果您想知道为什么这样~1-2,那是因为您正在反转一个有符号整数中的所有位;00000001变为1111110符号整数中的一个负数,请参见二进制补码

>>> # Python 3
...
>>> import struct
>>> format(struct.pack('b', 1)[0], '08b')
'00000001'
>>> format(struct.pack('b', ~1)[0], '08b')
'11111110'

其中起始1位表示该值为负,其余位则对正数减去一的值进行反编码。

The Python bool type is a subclass of int (for historical reasons; booleans were only added in Python 2.3).

Since int(True) is 1, ~True is ~1 is -2.

See PEP 285 for why bool is a subclass of int.

If you wanted the boolean inverse, use not:

>>> not True
False
>>> not False
True

If you wanted to know why ~1 is -2, it’s because you are inverting all bits in a signed integer; 00000001 becomes 1111110 which in a signed integer is a negative number, see Two’s complement:

>>> # Python 3
...
>>> import struct
>>> format(struct.pack('b', 1)[0], '08b')
'00000001'
>>> format(struct.pack('b', ~1)[0], '08b')
'11111110'

where the initial 1 bit means the value is negative, and the rest of the bits encode the inverse of the positive number minus one.


回答 2

~True == -2,如果不奇怪 True的手段1 ~方式按位反转

只要


编辑:

  • 修复整数表示和按位求反运算符之间的混合
  • 进行另一次抛光(信息越短,需要做的工作越多)

~True == -2 is not surprising if True means 1 and ~ means bitwise inversion

provided that


Edits:

  • fixed the mixing between integer representation and bitwise inversion operator
  • applied another polishing (the shorter the message, the more work needed)