我在上一门计算机系统课程,在某种程度上,我一直在为“二的补充”而挣扎。我想了解它,但我所读到的一切并没有让我明白它的含义。我读过维基百科的文章和其他各种文章,包括我的课本。

什么是2的补数,我们如何使用它,它如何在强制转换(从有符号到无符号,反之亦然)、逐位操作和位移位操作等操作中影响数字?


当前回答

Two的补码是一种存储整数的聪明方法,因此常见的数学问题很容易实现。

为了理解,你必须把数字想象成二进制。

它基本上是说,

对于0,用所有的0。 对于正整数,开始计数,最大值为2(位数-1)-1。 对于负整数,做完全相同的事情,但是切换0和1的角色并开始倒数(所以不是从0000开始,而是从1111开始——这是“补”部分)。

让我们尝试一个4位的迷你字节(我们称之为1/2个字节)。

0000 -零 0001 - 1 0010 - 2 0011 - 3 0100到0111,4点到7点

这是我们目前能找到的阳性结果。23-1 = 7。

负面影响:

1111 - 1 1110 - 2 1101 - 3 1100到1000 - - 4到- 8

注意,负数(1000 = -8)有一个额外的值,而正数没有。这是因为0000用于表示零。这可以看作是计算机的数轴。

区分正数和负数

这样一来,第一个位就扮演了“符号”位的角色,因为它可以用来区分非负的十进制值和负的十进制值。如果最高有效位是1,那么二进制就可以说是负的,如果最高有效位(最左边)是0,就可以说十进制值是非负的。

“符号量级”的负数只是将它们的正数对应的符号位颠倒了,但这种方法必须处理将1000(一个1后面跟着所有的0)解释为“负零”,这是令人困惑的。

“1的补”负数只是它们的正数的位补,这也导致了“负零”和1111(都是1)的混淆。

除非你的工作非常接近硬件,否则你可能不需要处理个位补或符号幅度整数表示。

其他回答

到目前为止,许多答案都很好地解释了为什么2的补数被用来表示负数,但没有告诉我们2的补数是什么,尤其是没有告诉我们为什么加了一个“1”,而且实际上经常以错误的方式加。

这种混淆来自于对补数定义的不理解。补语是指使某物完整的缺失部分。

根据定义,n位数x以b为基数的基数补是b^n-x。

在二进制中,4由100表示,它有3位数字(n=3)和基数2 (b=2)。所以它的基数补是b^n-x = 2^3-4=8-4=4(或二进制的100)。

然而,在二进制中,求一个基数的补并不像求它的消简基数补那么容易,消简基数补定义为(b^n-1)-y,只比基数补小1。要得到一个减少的基数补,只需翻转所有的数字。

100 -> 011(减基数补位)

为了得到基数(2的)补,我们只需按定义加1。

011 +1 ->100(2的补码)。

现在,有了这个新的理解,让我们看看Vincent Ramdhanie给出的例子(见上面的第二个回答):

将1111转换为十进制: 这个数从1开始,所以它是负的,所以我们找到1111的补数,也就是0000。 0000加上1,得到0001。 将0001转换为十进制,即1。 应用符号= -1。 大作。

应理解为:

数字从1开始,所以是负的。所以我们知道它是x的一个2的补。为了找到由它的2的补表示的x,我们首先需要找到它的1的补。

x的2的补数是1111 x的补数:1111-1 ->1110; X = 0001,(翻转所有数字)

应用符号-,结果=-x =-1。

2's complement is essentially a way of coming up with the additive inverse of a binary number. Ask yourself this: Given a number in binary form (present at a fixed length memory location), what bit pattern, when added to the original number (at the fixed length memory location), would make the result all zeros ? (at the same fixed length memory location). If we could come up with this bit pattern then that bit pattern would be the -ve representation (additive inverse) of the original number; as by definition adding a number to its additive inverse always results in zero. Example: take 5 which is 101 present inside a single 8 bit byte. Now the task is to come up with a bit pattern which when added to the given bit pattern (00000101) would result in all zeros at the memory location which is used to hold this 5 i.e. all 8 bits of the byte should be zero. To do that, start from the right most bit of 101 and for each individual bit, again ask the same question: What bit should I add to the current bit to make the result zero ? continue doing that taking in account the usual carry over. After we are done with the 3 right most places (the digits that define the original number without regard to the leading zeros) the last carry goes in the bit pattern of the additive inverse. Furthermore, since we are holding in the original number in a single 8 bit byte, all other leading bits in the additive inverse should also be 1's so that (and this is important) when the computer adds "the number" (represented using the 8 bit pattern) and its additive inverse using "that" storage type (a byte) the result in that byte would be all zeros.

 1 1 1
 ----------
   1 0 1
 1 0 1 1 ---> additive inverse
  ---------
   0 0 0

两人的补足(托马斯·芬利)

我把所有位的倒数加1。编程:

  // In C++11
  int _powers[] = {
      1,
      2,
      4,
      8,
      16,
      32,
      64,
      128
  };

  int value = 3;
  int n_bits = 4;
  int twos_complement = (value ^ ( _powers[n_bits]-1)) + 1;

简单来说,2的补码是一种在计算机内存中存储负数的方法。而正数则存储为普通二进制数。

让我们考虑这个例子,

计算机使用二进制数字系统来表示任何数字。

x = 5;

这表示为0101。

x = -5;

当计算机遇到-号时,它会计算出它的2的补数并存储它。

也就是说,5 = 0101,它的2的补是1011。

计算机处理数字的重要规则是,

如果第一位是1,那么它一定是负数。 如果除第1位之外的所有位都是0,那么它就是一个正数,因为在数字系统中没有-0(1000不是-0,而是正8)。 如果所有的位都是0,那么它就是0。 否则就是正数。

Two的补语主要用于以下原因:

避免0的多个表示形式 避免在溢出的情况下跟踪进位(如补位)。 进行简单的加法和减法运算变得很容易。