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.
Subtracting via adding negative binary numbers. Just do binary addition and it will work out. Throw away the extra carry overflow
6 8 4 2 1 0 1 1 0 -4 8 4 2 1 1 1 0 0 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) 8 4 2 1 0 0 1 0 Due to tossing away the overflow, we get 210, which is the answer to our subtraction problem in decimal: 6 + -4 = 2
| Decimal | Binary |
|---|---|
| -8 | 1000 |
| -7 | 1001 |
| -6 | 1010 |
| -5 | 1011 |
| -4 | 1100 |
| -3 | 1101 |
| -2 | 1110 |
| -1 | 1111 |
| 0 | 0000 |
| 1 | 0001 |
| 2 | 0010 |
| 3 | 0011 |
| 4 | 0100 |
| 5 | 0101 |
| 6 | 0110 |
| 7 | 0111 |
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!
We have to up to a 5 bit or more system because -8 + -8 = -16, which does not fit into a 4 bitsConvert 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
16 8 4 2 1 1 1 0 0 0
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