为什么二进制表示与 python 编译器不同,而不是我们在纸上所知道的?

Why the binary representation is different from python compiler than what we know on paper?(为什么二进制表示与 python 编译器不同,而不是我们在纸上所知道的?)
本文介绍了为什么二进制表示与 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 编译器不同,而不是我们在纸上所知道的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)