Binary subtraction is another fundamental operation used to subtract one binary number from another.
Just like in decimal subtraction, the corresponding bits of the numbers are subtracted from right to left, taking into account the borrows.
Binary subtraction can result in a negative result if the number to be subtracted is greater than the number from which it is being subtracted.
Try it
Borrow
The “borrow” occurs when you cannot subtract one number from another due to its value. This happens when the number being subtracted is greater than the number from which it is being subtracted.
In binary, this only happens when you try to subtract 1
from 0
. In this case, the result is ‘1’, and a value is “borrowed” from the next more significant bit.
Example
For example, to subtract the binary number 1101
(13 in decimal) from 1011
(11 in decimal):
1011
- 1101
______
1110
The result is 1110
, which is equal to -2 in decimal.
Binary subtraction with two’s complement
Binary subtraction is not difficult, but it can be a bit of a hassle. Generally, it is faster using the two’s complement method. This method allows you to convert the subtraction into an addition, and we know how to add two binary numbers “by hand” very quickly.
To subtract binary numbers using two’s complement, we follow these steps:
- Convert the number to be subtracted into its two’s complement
- Add the number from which you are subtracting with the two’s complement
- Discard any overflow bit at the end
We could also do a manual procedure to subtract by hand very quickly, as we did with addition. But in general, it is better to know two or three “tricks” to do things quickly, than to try to learn 27 and then mix them up or forget them all.
Example
Let’s subtract the binary number 1101
(13 in decimal) from the binary number 1011
(11 in decimal) using two’s complement:
1101
- 1011
_____
¿?¿?
First, we convert 1011
into its two’s complement. To do this, we invert all the bits and then add 1:
1011 (Original number) => Two's complement
0100 (Inverted bits)
+ 1 (Sum of 1)
_____
0101 (Two's complement of 1011)
We add
1101
with the two’s complement of1011
:1101 (Original number) + 0101 (Two's complement of 1011) ______ 1 0010 (Result)
By discarding the overflow bit to the left, we get the final result: 0010
, which is equal to 2 in decimal.
The advantages of using two’s complement for subtractions is that it simplifies the process and eliminates the need to manage the borrows.
Bonus: How to quickly subtract 1 in binary
Special bonus, how to quickly subtract 1 from a binary number. A very simple operation, but since it is very common, it is good to know how to do it.
Fortunately, subtracting 1 in binary “is easy”. Simply
- Traverse the binary number from the right
- Look for the first
1
you find, and change it to0
- From that position, to the right, all
1
s.
For example, if we had to subtract 1 from 1000100000
We look for the first 1
We change it to 0
and, from there to the right, all 1
s.
So the result is 1000011111
. As you can see, adding one is super fast.