Bitwise NOT 是第一个补码,例如:
Bitwise NOT is the first complement, for example:
x = 1
(二进制:0001)~x = -2
(二进制:1110)x = 1
(binary: 0001)~x = -2
(binary: 1110) 因此,我的问题是为什么二进制中的 -2 是 (-0b10) 与 python 编译器一样?
Hence, my question is why -2 in binary is (-0b10) as for the python compiler?
我们知道 1110 表示 (14) 代表无符号整数和 (-2) 代表有符号整数.
We know that 1110 represents (14) for unsigned integer and (-2) for signed integer.
二进制补码本质上取决于数字的大小.例如,有符号 4 位上的 -2 是 1110
,但有符号 8 位上是 1111 1110
.
Two's complement inherently depends on the size of a number. For example, -2 on signed 4-bit is 1110
but on signed 8-bit is 1111 1110
.
Python 的整数类型是任意精度的.这意味着没有明确定义的前导位来指示负号或二进制补码的明确长度.二进制补码是 1...1110
,其中 ...
是 1
的无限重复.
Python's integer type is arbitrary precision. That means there is no well-defined leading bit to indicate negative sign or well-defined length of the two's complement. A two's complement would be 1... 1110
, where ...
is an infinite repetition of 1
.
因此,Python 的整数显示为单独的符号(无或-
)和绝对数字.因此,-2
变为 -
和 0b10
- 即 -
2
.同样,-5
变成 -
和 0b101
——即 -
5
.
As such, Python's integer are displayed as a separate sign (nothing or -
) and the absolute number. Thus, -2
becomes -
and 0b10
– i.e. -
2
. Similarly, -5
becomes -
and 0b101
– i.e. -
5
.
请注意,此表示只是人类可读的标准表示.它不一定是实现定义的内部表示.
Note that this representation is merely the standard representation to be human-readable. It is not necessarily the internal representation, which is implementation defined.
这篇关于为什么二进制表示与 python 编译器不同,而不是我们在纸上所知道的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!