Why the binary representation is different from python compiler than what we know on paper?(为什么二进制表示与 python 编译器不同,而不是我们在纸上所知道的?)
问题描述
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 编译器不同,而不是我们在纸上所知道的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么二进制表示与 python 编译器不同,而不是我
基础教程推荐
- 包装空间模型 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
