简体   繁体   English

如何使用补码符号从负数中减去负数?

[英]How to subtract negative numbers from negative numbers using Complement notation?

I've just started learning.我刚开始学习。 I am trying to understand this thoroughly and deeply.我试图彻底而深入地理解这一点。 I somewhat understand it as long as it's subtraction between a Smaller number from a Larger number or a Larger number from a Smaller number, but when the Minuend in the question is also Negative it confuses me.我有点理解它,只要它是从较大的数字中减去较小的数字或从较小的数字中减去较大的数字,但是当问题中的被减数也是负数时,它会让我感到困惑。 Which value do I get the complement of?我得到哪个值的补码?

  1. When asked to do a problem such as, 0101 - 1100, when do I treat the 1 as a negative bit instead of just an unsigned bit?当被要求解决诸如 0101 - 1100 之类的问题时,我何时将 1 视为负位而不是无符号位? When would I read it as 5-12 instead of 5-(-3)?我什么时候会把它读成 5-12 而不是 5-(-3)?

  2. How do you solve 0101 - 1100 using One's Complement ?您如何使用One's Complement解决 0101 - 1100 问题? Is it possible?可能吗? Do I treat the question as asking me to subtract 12 from 5 instead of -3 from 5.我是否将这个问题视为要求我从 5 中减去 12 而不是从 5 中减去 -3。

  3. How do you subtract a number from a negative number?如何从负数中减去一个数? For example, -5-7 ?例如, -5-7 ? How do you do this using One's Complement only ?您如何仅使用 One's Complement来做到这一点? Do you get the complement of 7 or 5 or both and add them?你得到 7 或 5 或两者的补码并将它们相加吗? How would it change in Two's Complement?二进制补码会如何变化?

Please someone can clarify this for me, I want to understand this and move on from this.请有人可以为我澄清这一点,我想了解这一点并继续前进。

So after scouring other sources and getting some help, I have cleared all this up.因此,在搜索其他资源并获得一些帮助之后,我已经清除了所有这些。 I will answer it helps anyone that comes across this.我会回答它可以帮助遇到这个问题的任何人。

1. Identifying whether to read 0101 - 1100 as 5-12 or 5-(-3) can only be done if there is context given as to whether it's signed or unsigned. 1.识别 0101 - 1100 是读作 5-12 还是 5-(-3) 只能在给出关于它是有符号还是无符号的上下文的情况下才能完成。

2. With Ones complement, 0101 - 1100 = 5-(-3) = 5+3. 2.用一个补码,0101 - 1100 = 5-(-3) = 5+3。 We get Ones complement of 1100 by negating/inverting = 0011 = 3. Then we do addition:我们通过取反/取反 = 0011 = 3 得到 1100 的补码。然后我们进行加法:

   5+3 = 0101 + 0011 = 1000        

The answer is not 8 and is a negative result.答案不是 8,结果是否定的。 This means it has overflowed, because we added two values of the same sign and got a result with the opposite sign.这意味着它已经溢出,因为我们添加了两个相同符号的值并得到了相反符号的结果。 The overflow occurs because 4 bits Ones complement has a range of -7 to +7.发生溢出是因为4位二进制补码的范围为 -7 到 +7。 The 1 in 1000 occupies the positional value -(2^( 4 -1))-1 which is -7. 1000 中的 1 占据位置值 -(2^( 4 -1))-1,即 -7。 So it has gone from +7 to -7 with the extra 1 value (8-7 = +1), meaning the -7 is essentially +7 + 1 and that additional 1 has overflowed into -7.所以它已经从 +7 变为 -7 并带有额外的 1 值(8-7 = +1),这意味着 -7 本质上是 +7 + 1 并且额外的 1 溢出到 -7 中。 This because the 8th value in the range starting from 0 is -7 in 4 bits Ones complement representation.这是因为从 0 开始的范围内的第 8 个值在 4 位反码表示中是 -7。 Consider this:考虑一下:

Values from -7 to +7 can form a wheel. -7,-6,-5,-4,-3,-2,-1,-0,0,1,2,3,4,5,6,7
If you keep adding 1 to each value above it will take you to the next value and 
keep going in a circle in binary form.
Example:

+7 = 0111
0111 + 1 = 1000 = -7
1000 + 1 = 1001 = -6 
1001 + 1 = 1010 = -5 and on on and on.

So the calculation is correct, just that the 4 bit ones complement representation cannot represent a +8 and overflows to a -7.所以计算是正确的,只是 4 位的补码表示不能表示 +8 并溢出到 -7。

**3.**In this case it underflows , where the negative number of the -5-7 is too low to be represented with Ones Complement and Twos Complement in 4 bits representation. **3.**在这种情况下,它会下溢,其中 -5-7 的负数太低而无法用 4 位表示的 Ones Complement 和 Twos Complement 表示。

-5 in Ones' Complement = 1010,    -7 in Ones' Complement = 1000
-5-7 = -a-b = -(a+b)
So mathematically you can look at it as 1010+1000 or -(0101+0111)
          1010                                      0101
         +1000                                     +0111
=Carry->1 0010                                  = -(1100)= -(-3)= 3  
         +   1
=         0011= 3

So we end up with the same answer regardless, I'd recommend the first method though so that it doesn't lead to confusion.因此,无论如何,我们最终都会得到相同的答案,但我会推荐第一种方法,以免引起混淆。 The result is +3 after adding two negative values, and the value itself is 3, which shows that it has underflowed because -12 cannot be represented in 4 bit Ones Complement as the values are only from -7 to +7 as mentioned above.两个负值相加后的结果是+3,而值本身是3,这表明它已经下溢,因为-12不能用4位补码表示,因为值只是从-7到+7,如上所述。 So referring to the list of the range of values above.因此,请参阅上面的值范围列表。 If you count backwards 7 units you end up at +3.如果你倒数7 个单位,你最终会得到 +3。 If the range of values contained -12 value then counting back from -5 will have you end up at -12.如果值的范围包含 -12 值,那么从 -5 往回计数将使您最终得到 -12。

With Twos complement , the difference is that you add 1 to the ones complement values to get Two's Complement.使用二进制补码,不同之处在于您将 1 添加到二进制补码值以获得二进制补码。 Then add them the same way.然后以相同的方式添加它们。

-5 in Ones' Complement = 1010,    -7 in Ones' Complement = 1000
      Two's Complement = 1011,          Two's Complement = 1001
          
                  1011                                     
                 +1001                                     
= Overflow bit->1 0100 = +4

In Two's Complement calculation the Overflow bit is ignored if you get it when adding two values of different signs, or when adding the same sign you get an answer with the same sign.在二进制补码计算中,如果在添加两个不同符号的值时得到溢出位,或者在添加相同符号时得到相同符号的答案,则会忽略溢出位。 In the above case however, it is 0100 which is +4, after adding two negative values.然而,在上述情况下,在添加两个负值之后,它是 +4 的 0100。 So it indicates overflow.所以它表示溢出。

The reason the value is +4 instead of +3 like in Ones' Complement is because there is an extra negative value in Two's Complement range compared to One's Complement as Two's Complement doesn't have a negative 0.值是 +4 而不是像 Ones' Complement 中的 +3 的原因是,与 One's Complement 相比,Two's Complement 范围内有一个额外的负值,因为 Two's Complement 没有负 0。

The range of values for Two's Complement 4 bit representation is:
-2^(4-1) to +(2^(4-1))-1 which is equal to -8 to +7

-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7

If you count backwards 7 units from -5, you end up at +4. Since -12 cannot still 
be represented between -8 to +7, +4 is where you end up with the overflow.

I hope this clears up things and helps someone.我希望这可以解决问题并帮助某人。 Please let me know if there are any mistakes and I'll correct them.如果有任何错误,请告诉我,我会纠正它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM