简体   繁体   中英

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? When would I read it as 5-12 instead of 5-(-3)?

  2. How do you solve 0101 - 1100 using One's Complement ? Is it possible? Do I treat the question as asking me to subtract 12 from 5 instead of -3 from 5.

  3. How do you subtract a number from a negative number? For example, -5-7 ? How do you do this using One's Complement only ? Do you get the complement of 7 or 5 or both and add them? 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.

2. With Ones complement, 0101 - 1100 = 5-(-3) = 5+3. We get Ones complement of 1100 by negating/inverting = 0011 = 3. Then we do addition:

   5+3 = 0101 + 0011 = 1000        

The answer is not 8 and is a negative result. 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. The 1 in 1000 occupies the positional value -(2^( 4 -1))-1 which is -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. This because the 8th value in the range starting from 0 is -7 in 4 bits Ones complement representation. 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.

**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.

-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. So referring to the list of the range of values above. If you count backwards 7 units you end up at +3. If the range of values contained -12 value then counting back from -5 will have you end up at -12.

With Twos complement , the difference is that you add 1 to the ones complement values to get Two's Complement. 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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