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

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


当前回答

2对给定数的补数是1与1的补数相加得到的数。

假设我们有一个二进制数:10111001101

它的1的补位是:01000110010

它的2的补数是:01000110011

其他回答

我想知道是否有比维基百科上的文章更好的解释。

你试图用2的补表示法解决的基本问题是存储负整数的问题。

首先,考虑一个存储在4位的无符号整数。您可以拥有以下内容

0000 = 0
0001 = 1
0010 = 2
...
1111 = 15

这些是无符号的,因为没有指示它们是负的还是正的。

符号大小和多余符号

要存储负数,您可以尝试一些方法。首先,您可以使用符号幅度表示法,它将第一个位指定为符号位来表示+/-,其余位表示幅度。还是用4位假设1代表- 0代表+那么你就有

0000 = +0
0001 = +1
0010 = +2
...
1000 = -0
1001 = -1
1111 = -7

所以,你看到问题了吗?我们有正0和负0。更大的问题是二进制数的加减法。使用符号幅度进行加减法的电路将非常复杂。

是什么

0010
1001 +
----

?

另一个系统是过量符号。你可以存储负数,你可以摆脱两个0的问题但加减法仍然很困难。

于是就有了二的补。现在您可以存储正整数和负整数,并相对轻松地执行算术。有许多方法可以将一个数转换为二的补数。这是一个。

将十进制转换为二的补数

将数字转换为二进制(暂时忽略符号) 例如,5是0101,-5是0101 如果这个数字是正数,那么你就完成了。 例5是二进制的0101,使用二的补符号。 如果数字是负的,那么 3.1求补(0和1的倒数) 例如,-5是0101,所以找到补语是1010 3.2补数1010 + 1 = 1011加1 因此,2的补数-5等于1011。

那么,如果你想用二进制写2 +(-3)呢?2 +(-3) = -1。 如果你用符号的大小来加这些数,你需要做什么?0010 + 1101 = ?

使用2的补码,想想会有多简单。

 2  =  0010
 -3 =  1101 +
 -------------
 -1 =  1111

将2的补数转换为十进制

将1111转换为十进制:

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

哒哒!

补一词来源于完备性。在十进制世界中,数字0到9提供了一个数字或数字符号的补集(完整集)来表示所有的十进制数。在二进制世界中,数字0和1提供了一个数字的补数来表示所有二进制数。事实上,符号0和1必须用来表示所有东西(文本、图像等)以及正(0)和负(1)。 在我们的世界里,数字左边的空白被认为是零:

                  35=035=000000035.

In a computer storage location there is no blank space. All bits (binary digits) must be either 0 or 1. To efficiently use memory numbers may be stored as 8 bit, 16 bit, 32 bit, 64 bit, 128 bit representations. When a number that is stored as an 8 bit number is transferred to a 16 bit location the sign and magnitude (absolute value) must remain the same. Both 1's complement and 2's complement representations facilitate this. As a noun: Both 1's complement and 2's complement are binary representations of signed quantities where the most significant bit (the one on the left) is the sign bit. 0 is for positive and 1 is for negative. 2s complement does not mean negative. It means a signed quantity. As in decimal the magnitude is represented as the positive quantity. The structure uses sign extension to preserve the quantity when promoting to a register [] with more bits:

       [0101]=[00101]=[00000000000101]=5 (base 10)
       [1011]=[11011]=[11111111111011]=-5(base 10)

用作动词: 2的补语表示否定。这并不意味着消极。意思是如果负数变成正数;如果是正的就是负的。大小是绝对值:

        if a >= 0 then |a| = a
        if a < 0 then |a| = -a = 2scomplement of a

此功能允许使用先求负后加的有效二进制减法。 A -b = A + (-b)

1的补数的官方方法是每一位数用1减去它的值。

        1'scomp(0101) = 1010.

这与逐个翻转或反转每一位是一样的。结果是- 0,这是不受欢迎的,所以给te 1的补码加上1就解决了这个问题。 要求2s的补,先求1s的补,然后加1。

        Example 1                             Example 2
         0101  --original number              1101
         1's comp  1010                       0010
         add 1     0001                       0001
         2's comp  1011  --negated number     0011

在这些例子中,否定也适用于符号扩展数。

添加: 1110进位111110进位 0110与000110相同 1111年 111111年 Sum 0101 Sum 000101

减法:

    1110  Carry                      00000   Carry
     0110          is the same as     00110
    -0111                            +11001
  ----------                        ----------
sum  0101                       sum   11111

请注意,当使用2的补码时,数字左侧的空白区域对于正数用0填充,而对于负数用1填充。进位总是被加上,必须是1或0。

干杯

我在Reddit上读到jng的一篇精彩的解释,用里程表做类比。

It is a useful convention. The same circuits and logic operations that add / subtract positive numbers in binary still work on both positive and negative numbers if using the convention, that's why it's so useful and omnipresent. Imagine the odometer of a car, it rolls around at (say) 99999. If you increment 00000 you get 00001. If you decrement 00000, you get 99999 (due to the roll-around). If you add one back to 99999 it goes back to 00000. So it's useful to decide that 99999 represents -1. Likewise, it is very useful to decide that 99998 represents -2, and so on. You have to stop somewhere, and also by convention, the top half of the numbers are deemed to be negative (50000-99999), and the bottom half positive just stand for themselves (00000-49999). As a result, the top digit being 5-9 means the represented number is negative, and it being 0-4 means the represented is positive - exactly the same as the top bit representing sign in a two's complement binary number. Understanding this was hard for me too. Once I got it and went back to re-read the books articles and explanations (there was no internet back then), it turned out a lot of those describing it didn't really understand it. I did write a book teaching assembly language after that (which did sell quite well for 10 years).

从数学的角度来看这两个补体系统是有道理的。在ten的补语中,这个想法本质上是“隔离”差异。

示例:63 - 24 = x

我们把24的补数相加,也就是(100 - 24)实际上,我们要做的就是在方程两边加100。

现在方程是:100 + 63 - 24 = x + 100,这就是为什么我们要去掉100(或10或1000或其他)。

由于必须从一长串零中减去一个数字的不方便情况,我们使用“减基数补”系统,在十进制系统中,9的补。

当我们看到一串大的9减去一个数时,我们只需要把数字倒过来。

例如:99999 - 03275 = 96724

这就是为什么在9的补数之后加1。你可能从儿时的数学中知道,9通过“偷走”1变成了10。所以基本上就是10的补位差减去1。

在二进制中,2的补数等于10的补数,而1的补数等于9的补数。主要的区别在于,我们不是试图用10的幂来分离差异(将10、100等添加到等式中),而是试图用2的幂来分离差异。

正是因为这个原因,我们把比特位颠倒。就像小数中的被减数是一串9一样,二进制中的被减数也是一串1。

例如:111111 - 101001 = 010110

因为1链比2的幂小1,它们从差值中“偷”了1,就像小数点中的9一样。

当我们使用负二进制数时,我们实际上是在说

0000 - 0101 = x

1111-0101 = 1010

1111 + 0000 - 0101 = x + 1111

为了“分离”x,我们需要加1,因为1111离10000只有1,我们去掉前导的1,因为我们只是把它加到原始的差值上。

1111 + 1 + 0000 - 0101 = x + 1111 + 1

10000 + 0000 - 0101 = x + 10000

只要两边都去掉10000就得到x,这是基本的代数。

你也可以使用在线计算器来计算一个十进制数的补二表示:http://www.convertforfree.com/twos-complement-calculator/