一种思考方式是,带符号的二进制补码格式的工作原理是为每个位分配 2 的幂,然后翻转最后一个 2 的幂的符号。例如,我们看-4,它表示为 100。这意味着该值为
-1 x 2^2 + 0 x 2^1 + 0 x 2^0
如果我们想得到这个值的正数,我们必须对它求反才能得到
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 解释为有符号的三位二进制补码值时,它的返回值与我们开始时的值相同。比特的短缺是这里的问题所在。
更一般地,给定 n 位,其中第一个是二进制补码表示中的符号位,尝试计算 -1000...00 将返回相同的值,因为存储大正值所需的位具有特殊的赋予它的意义。
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.
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.
希望这可以帮助!