Digital subtractor
Summary
In this chapter, I’ll explain how the computer hardware is calculating “a - b”. To understand how subtractor works, binary adder has to be understood. This is explained in the past article.
How the computer calculates “a + b”
Decimal subtraction
In the computer hardware, subtraction is calculated differently. I’ll show example using decimal number.
We’ll calculate “7 - 3” like computer. First, we need to find the number which is called 10’s compliments. Compliment of 3 is calculated like below.
3 + X = 10
X = 10 - 3
So 10’s compliment of 3 is 7. Next, add this number with 7, then ignore 10’s digit.
7 + 7 - 10 = 4
This is basically the idea. What we did is actually the calculation below.
7 + (10 - 3) - 10
In case of decimal, this is more complicated. But if this is binary, compliment is easily calculated (bold part above). Also, another point is, subtraction is calculated by adder.
2’s compliment
In binary, compliment is calculated in this way.
- Flip 0 and 1 in all the digits.
- Add 1.
That’s it. For example, compliment for 0011 (3 in decimal) is calculated like below.
- Flip 0 and 1 … 0011 -> 1100
- Add 1 … 1101
Let’s calculate “7 - 3” in binary. 7 is 0111 in binary, and 3 is 0011. Compliment of 3 is already calculated (1101). Add this number with 7 (0111).
0111 + 1101 = 1 0100
If we ignore the left most digit, it’s “0100”, which is 4 in decimal. For this reason, negative values are expressed as compliments in digital hardware.
Digital subtractor
As I explained above, to calculate “a - b”, we will use adder. The block diagram of 4-bit adder is shown below.
The point is, we were using 1 half-adder, and 3 full-adders, because first digit never gets carry from anywhere. But, if we replace this half-adder with full-adder, we can easily calculate “+1” to get compliment of input “B”.
Next, flipping bit is easy. NOT gates are added to all the digit of input “B”. 4-bit binary subtractor is shown below.
Also, we need to ignore the most significant bit Y4. This is it.
But look at this hardware carefully…we are sharing almost the same hardware with adder. So if we add a bit of improvements, it can be used as adder as well.
Bit negation
We used NOT gate to negate input B in the example above, but we can use XOR instead. Basically, the output of XOR is 1, if “one of A or B is 1, but not both”. Instead, we can see it differently. Let’s say, XOR has two inputs “X” and “INV”, and output “Y”. Let’s think about the case where “INV is always 0”. In this case, Y = 0 if X = 0, and Y = 1 if X = 1. So “if INV = 0, Y = X”. Next, imagine the case where, “INV is always 1”. In this case, Y = 1 if X = 0, and Y = 0 if X = 1. So we can say, “if INV = 1, Y = NOT X”.
Replace NOT gates with XOR gates, and connect one of the inputs to the signal “SUB”. Also, connect input to the carry of first bit to the signal “SUB”, we can use this hardware as both adder and subtractor. If the signal “SUB” is “0”, this works as adder, and if “SUB” is “1”, this hardware works as subtractor.