Morgan Wilde
Morgan Wilde

Reputation: 17323

How to subtract from a smaller number in binary?

I have two numbers. The negative one is translated using 2's complement.

a = 160 = A0 = 1010 0000
b = -28 = E4 = 1110 0100

And I have to perform the following operation a - b. I use borrowing up to the very last bit, but then I have a situation like.

      0 1
    - 1 1
      ---
      ? 0

Should I borrow from something imaginary? In decimal 1 - 3 = -2, so the answer is BC, but how do you reason about the negative 2 here?

Upvotes: 0

Views: 935

Answers (1)

mch
mch

Reputation: 9814

If you are doing 2's complement calculation, both numbers are 2's complement, so A0 is equal to decimal -96 and not 160.

160 is not representable in 8bit 2's complement. I will use 9bit 2's complement here.

a - b where a=160 and b=-28 is equal to 160 + 28. Is that what you want?

  0 1010 0000
+ 0 0001 1100
-------------
  0 1011 1100

or do you want to calculate 160 - 28? this is equal to 160 + (-28)

  0 1010 0000
+ 1 1110 0100
-------------
 10 1000 0100

we can ignore the left 1, because we are doing 9bit 2's complement calculation. So the result is 0 1000 0100 which is 132.

This is the advantage of the 2's complement, you don't have to do a subtraction, you can add the 2's complement of the number instead of subtract it.

8bit 2's complement with the original binaries:

  1010 0000
+ 1110 0100
  ---------
 11000 0100

the result is 1000 0100 which is -124 (-96 - 28).

Upvotes: 2

Related Questions