问题描述
我仍然没有找到为什么最小的有符号负数没有等效的有符号正数的原因?为简单起见,我的意思是 3 位二进制数100是-4?但我们不能有一个正4的签名格式,因为我们不能.它溢出了.那么我们如何知道二进制补码 1000 是 -4 1000 0000 是 -128 等等?我们没有原始的正数
I still haven't found a reason why the lowest signed negative number doesn't have an equivalent signed positive number? I mean in a 3 digit binary number for simplicity 100 is -4? but we can't have a positive 4 in signed format because we can't. It overflows. So how do we know two's complement 1000 is -4 1000 0000 is -128 and so on? We have no original positive number
推荐答案
一种思考方式是有符号的二进制补码格式的工作原理是为每个位分配一个 2 的幂,然后翻转最后一个 2 的幂的符号.我们以-4为例,它表示为100.这意味着该值为
One way to think about it is that signed, two's complement format works by assigning each bit a power of two, then flipping the sign of the last power of two. Let's look at -4, for example, which is represented as 100. This means that the value is
-1 x 2^2 + 0 x 2^1 + 0 x 2^0
如果我们想得到这个值的正数,我们必须否定它才能得到
If we want to get the positive version of this value, we'd have to negate it to get
1 x 2^2 - 0 x 2^1 - 0 x 2^0
注意这个值等于
1 x 2^2 + 0 x 2^1 + 0 x 2^0
换句话说,这个值的正常二进制表示是 100.但是,我们在这里遇到了麻烦,因为我们使用带符号的二进制补码表示,这意味着我们专门保留了 4 位作为符号少量.因此,当我们尝试将位模式 100 解释为带符号的三位二进制补码值时,它会返回与我们开始时相同的结果.位的短缺是这里的痛点.
In other words, the normal binary representation of this value is 100. However, we're in trouble here, because we're using a signed two's complement representation, which means that we have specifically reserved the 4's bit as the sign bit. Consequently, when we try to interpret the bit pattern 100 as a signed, three-bit, two's complement value, it comes back out identically to what we started with. The shortage of bits is what's hurting here.
更一般地,给定 n 位,其中第一个是二进制补码表示中的符号位,尝试计算 -1000...00 将返回相同的值,因为存储大正值所需的位具有赋予它的特殊含义.
More generally, given n bits, of which the first is the sign bit in a two's complement representation, trying to compute -1000...00 will give back the same value, because the bit needed to store the large positive value has the special meaning assigned to it.
那么为什么要这样做呢?这样做的原因是,如果只有 n 位,则无法存储值 -2n - 1 到 2n - 1,因为有 2n + 1 个不同的数字,只有 2^n 个不同的位模式.因此,排除最大的正数可以在指定的位模式中保存所有不同的数字.
So why do this at all? The reason for this is that if you have only n bits, you cannot store the values -2n - 1 through 2n - 1, because there are 2n + 1 different numbers here and only 2^n different bit patterns. Excluding the largest positive number thus makes it possible to hold all the different numbers in the bit pattern specified.
但是为什么要放弃高价值而不是低价值呢?这是为了保持与无符号整数的二进制兼容性.在无符号整数中,值 0 到 2n-1 - 1 都使用标准的以二为底的表示进行编码.因此,为了使无符号整数和有符号整数完全一致,无符号整数被设计为与前 2n - 1 个无符号整数逐位等效,范围从 0 到 2n - 1 - 1,包括在内.在此之后,无符号值需要最高有效位来编码数字,但有符号值使用此作为符号位.
But why drop the high value and not the low value? This is in order to preserve binary compatibility with unsigned integers. In an unsigned integer, the values 0 through 2n-1 - 1 are all encoded using the standard base-two representation. Consequently, for unsigned and signed integers to agree at all, the unsigned integers are designed so that they are bit-for-bit equivalent with the first 2n - 1 unsigned integers, which range from 0 to 2n - 1 - 1, inclusive. After this, the unsigned values need the most significant bit to encode numbers, but the signed values are using this as the sign bit.
希望这会有所帮助!
这篇关于为什么 -INT_MIN = INT_MIN 在有符号的二进制补码表示中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!