Binary Subtraction

#How to subtract binary numbers?

x + y = x + -y


Subtraction is just addition with negative numbers! To put it shortly, if you already know how to add two binary numbers, you know how to subtract.
In decimal for example:

6 - 4 = 6 + -4
2 = 2


Now let’s do this in binary.

  1. Subtracting via adding negative binary numbers. Just do binary addition and it will work out. Throw away the extra carry overflow
    6
    8421
    0110
    -4
    8421
    1100
      1 1
      [ 0 1 1 0 ] (6)
    + [ 1 1 0 0 ] (-4)
    _______________
    1 [ 0 0 1 0 ] (2)
    ^ overflow
    

    the overflow is ignored because it can’t fit inside the bit width.

    2 in binary (signed)
    8421
    0010

    Due to tossing away the overflow, we get 210, which is the answer to our subtraction problem in decimal: 6 + -4 = 2

signed
DecimalBinary
-81000
-71001
-61010
-51011
-41100
-31101
-21110
-11111
00000
10001
20010
30011
40100
50101
60110
70111


Extra Examples
Example: -8 + 3 = -5

  [ 1 0 0 0 ] (-8)
+ [ 0 0 1 1 ] (3)
_______________
[ 1 0 1 1 ] (-5)


Example: -6 + -1 = -7

  1 1 1
  [ 1 0 1 0 ] (-6)
+ [ 1 1 1 1 ] (-1)
_______________
1 [ 1 0 0 1 ] (-7)

Check your understanding!

Convert these numbers to binary and add them: -5 + 4
  [ 1 0 1 1 ] (-5)
+ [ 0 1 0 0 ] (4)
_______________
[ 1 1 1 1 ] (-1)

Convert these numbers to binary and add them: 7 + -7
  1 1 1 1
  [ 0 1 1 1 ] (7)
+ [ 1 0 0 1 ] (-7)
_______________
1 [ 0 0 0 0 ] (0)

Convert these numbers to binary and add them: -8 + -8

We have to up to a 5 bit or more system because -8 + -8 = -16, which does not fit into a 4 bits

-8 in 5bit binary
168421
11000
  1 1
  [ 1 1 0 0 0 ] (-8)
+ [ 1 1 0 0 0 ] (-8)
_______________
1 [ 1 0 0 0 0 ] (-16)


Fun Fact: New hardware is not required for subtraction, we just re-use what we do in addition!

More Signed Binary Subtraction problems with variable bitwidth

#Overflow Problems


There are a few problems with signed arithmetic.

Positive overflow:

Positive + Positive = Negative
Adding two positive numbers can result in a negative value

  0110 (6)
+ 0011 (3)
___________
  1001 (-7)

Negative overflow:

Negative + Negative = Positive
Adding two negative numbers will result in a positive

 1 11 
  1011 (-5)
+ 1011 (-5)
________
  0110 (6)


If you’re adding two signed numbers of opposite sign, e.g positive + negative, you will never overflow


More Signed Overflow Problems