Creating a subtractor with logic gates
We previously saw how to add two binary numbers using logic gates. In that article, we developed a simple circuit to add two binary bits, with carry-in and carry-out bits. We also saw how to combine multiple single-bit adders to create a circuit that can add binary numbers together.
In this article, we will extend that to cover subtraction too. This is easier than you might expect. We will also look at how to make a single circuit that can perform addition, subtraction, and negation.
Binary subtraction
We covered binary addition and subtraction here. To recap, in the case of a 4-bit word, we can either treat it as an unsigned number or a signed number:
If we are using unsigned numbers, the binary numbers 0000 to 1111 represent denary numbers 0 to 15, following the normal binary notation.
If, instead, we are using signed numbers, binary values 0000 to 0111 still represent denary numbers 0 to 7. But binary values 1000 to 1111 represent denary numbers -8 to -1.
We can use this to perform subtraction by doing addition. For example, to subtract 3 from 5, we can add -3 to 5. Since -3 signed is equivalent to 13 unsigned, this wraps around to give the correct answer, 2: