Binary Subtraction with 2's Complement

本秂侑毒 提交于 2019-12-21 05:32:11

问题


I need help subtracting with binary using 2's representation and using 5 bits for each number:

1) -9 -7 = ? Is there overflow?

-9 = 01001 (2's complement = 10111) and -7 = 00111 (2's complement = 11001)

Now we need to add because we're using 2's complement

10111 +11001 = 100000 But this answer doesn't make sense. Also, I'm assuming there's overflow because there are more than 5 bits in the answer.

2) 6 - 10, same process as before. Negative binary numbers don't make sense to me


回答1:


1) -9 - 7

-9 - 7 = -9 + -7

9 (binary) = 01001
-9 (2's complement) = 10111
7 (binary) = 00111
-7 (2's complement) = 11001

 10111 +
 11001 =
110000

This doesn't fit into 5 bits. Removing the overflow we get 10000, which is -16 (binary).

2) 6 - 10

6 - 10 = 6 + -10

6 (binary) = 00110
10 (binary) = 01010
-10 (2's complement) = 10110

 00110 +
 10110 =
 11100

This fits into 5 bits and is -4 (binary).




回答2:


10111 + 11001 is not 100000 but 110000.

   1111
   10111
 + 11001
   -----
  110000



回答3:


Answer to the 1st question is wrong. To find -9-7 using two's complement, we need follow these steps:

STEP:1 Convertion of first  number

1st the binary conversion of 9:     01001

2nd find the complement of binary:  10110

Add 1 to the binary complement:     10110  
                                       +1
                                    -----
                                    10111  
STEP 2: Convertion of second number

1st the binary conversion of 7:     00111

2nd find the complement of binary:  11000

Add 1 to the binary complement:     11000 
                                       +1
                                   -------
                                    11001
STEP 3: Adding

Now add the two outputs -9 + (-7):  10111
                                   +11001
                                  --------
                                   110000
The most important thing is checking the answer whether it is correct or not.
you may use index for the binary digits:       7 6 5 4 3 2 1 0
                                                   1 1 0 0 0 0

find the 2 raised to the power of each index having 1 digit.
                                                (-)2^5 + 2^4

*Note (-) is used because in two's complement, the most significant bit (the bit with the highest index) is a sign bit -2^5 + 2^4 = -32 + 16 = -16 which is the correct answer for -9-7=-16. For this reason 2's complement become a popular way of representing negative number. For sign magnitude we need to assume a sign bit, which is hard to implement in a computer, and for 1's complement we need to add 1 to find the correct answer.



来源:https://stackoverflow.com/questions/14429784/binary-subtraction-with-2s-complement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!