3

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

Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
iOSDev
  • 1,028
  • 4
  • 13
  • 21
  • 2
    This *technically* is not a programming question. – m0skit0 Jan 20 '13 at 21:57
  • What is the expected answer? How would it be represented in 2s complement? Does it fit in the provided digits? – dmckee --- ex-moderator kitten Jan 20 '13 at 22:01
  • 2
    Use 8-bits rather than 5-bits, to avoid confusion. This approach will surely help. `-9(binary) : 0000 1001` & `-9(2's complement) : 1111 0111`. `-7(binary) : 0000 0111` & `-7(2's complement) : 1111 1001`. Now on adding these two: `1111 0111 +1111 1001` will yield `1` as carry & `1111 0000` as the output. The leftmost `1` in `1111 0000`, indicates the output is `-(negative)`. So now take the complement of the output & add `1` to it. That is your answer in binary. Your Question can be inferred as: `(-9)-(+7)`. So you don't have to waste time in deducing 2's complement of `-7`. – phougatv Nov 21 '14 at 07:20

3 Answers3

7

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

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • I understand computer store negative numbers by their 2's complement. I'm wondering what if `6 - a, where a == -10`. How does this process of `-(-10)` happen? Does it try to get the 2's complement of -10 (10110) ? – Weishi Z Mar 25 '17 at 00:19
  • @WeishiZeng I don't know how real processors do it. I like to transform `x - y` into `x + -y` because then I don't need to remember a separate algorithm for subtracting binary numbers. If you have `00110 - 10110`, that becomes `00110 + -10110`, `00110 + 01010`, `10000`, which is -16. So yes, I simply take the 2's complement of `10110` by flipping all bits and adding `1`. – melpomene Mar 25 '17 at 08:59
0

10111 + 11001 is not 100000 but 110000.

   1111
   10111
 + 11001
   -----
  110000
m0skit0
  • 25,268
  • 11
  • 79
  • 127
0

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.

dhaval nagar
  • 111
  • 11