为什么在-INT_MIN = INT_MIN签约,补重presentation? [英] Why does -INT_MIN = INT_MIN in a signed, two's complement representation?

查看:219
本文介绍了为什么在-INT_MIN = INT_MIN签约,补重presentation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有找到一个原因最低签署负数不具备等效的有符号正数?
我的意思是在为简单起见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

推荐答案

要想想方法之一是签署,二进制补码格式的作品通过分配每个比特的两个电源,然后翻转两个最后一次加电的符号。让我们来看看-4,例如,这是重新psented为100 $ P $这意味着值为

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

在换句话说,这个值的正常二进制重新presentation是100。但是,我们在这里遇到麻烦,因为我们使用的是符号的二进制补重presentation,这意味着我们有特别保留的4比特作为符号位。因此,当我们试图跨preT作为签约,三位,补值的位模式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位,其中第一个是在一个二的补码重新presentation符号位,试图计算-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位,则不能存储值-2 N - 1 通过2 N - 1 ,因为有2 ñ这里+ 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.

但为什么降高值,而不是低价值?这是为了与无符号整数preserve二进制兼容性。在一个无符号整数,值从0到2 N-1 - 1均设有连接使用标准的基二重presentation codeD。因此,对于无符号和符号整数在都同意,无符号整数的设计,使它们是位对位等效与第2 N - 1 无符号整数,其范围从0到2 N - 1 - 1,包容。在此之后,无符号值需要连接code号最显著位,但签署值以此为符号位。

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签约,补重presentation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆