Digital Logic Tutorial

Binary Subtractor for 5 digits

So we can add using only NAND gates, but can we subtract? From math, you know that subtracting is just adding a negative. A-B is the same as A+(-B). And making a negative in binary math is really easy: Just take each digit, and invert it, then add one to the resulting number. To invert, we just run each digit though a NOT gate (formed from a NAND with both inputs wired together). Then, adding 1 is easy, since our adder already has a carry input and all a carry in really does is add 1 or 0 to the result. (we will add 1). This "negative of the number" is also called the "additive inverse^" because it's the number you add to make another number go away.

The circuit below is the result, The inputs are from the left. To save space, we just used one number box. The number on the right is subtracted from the number on the left and the result is displayed on the far right. Use the up and down arrows to change the numbers.

If you look back at the adder, you will see that this is almost exactly the same circuit. The only difference (other than using just one input  block) is that the wires for one of the numbers goes through that set of NANDs wired as NOTs. And notice the push button at the top. We have that wired into the carry input of the adder, and pre-set to a 1. This adds the one we needed to make our negation into a negative.

To view this in the simulator, remember to enable Java in your browser:
https://java.com/en/download/help/enable_browser.xml

If you stare at that long enough, you might start to see the paterns of how the logic works. You can go back to the simple adder to see what is inside each box. Remember: It's all done with NANDs!

Next

The 2 bit to 4 line active low decoder puts things in order.

Advanced

We aren't /really/ finding the negative number by negating and adding one, but it's close enough. What we are doing is finding the number to add that makes the system exactly overflow.

Here is a decimal example: What number, when added to a three digit number, always makes 10000? For 1111, it's 8889, because 1111+8889=10000. note that 8889 is 8888 +1. You can always find the additive overflow by subtracting each digit of the original number from 9, then adding 1 to the result. For 1234 it's 8765+1 or 8766. For 4567 it's 5432+1 or 5433. 2915 it's 7085. What is it for 9912? (mouse over for the answer)

Notice that the result, 10000 has one more digit, always a 1, and the original 4 digits are now zero. This is what we are doing when we negate and add one to a binary number: We are finding the binary number that makes the original digits zero, with one additional digit. For 0101 (5 decimal), the answer is 1010+1 or 1011 (11 decimal), because 0101+1011=10000 in binary math (5+11=16).

But in digital logic math, we only have so many wires, so that extra digit is lost (or becomes a carry out). So what did we really do if we always ignore the extra top digit? We found the number to ADD which makes the original number zero. And what is a number you add that makes the original zero? It's the negative of that number: A+(-A)=0.

See also:

Questions:

The explanation I found easiest to understand is that of the "additive inverse" or the number you add to something to make it "go away" or rather, to make all but the highest digit zero. This is effectively the negative of the number if you drop the top digit.

So, the inverse of 25 is 75, because 25+75=100 and you drop the 1. So to subtract 25 from, say, 50, you add 75 instead and get 125, drop the 1, 25 is 50-25.

Another example: The inverse of 2 is 8 because 2 and 8 are 10 (drop the 1). So to subtract 2 from 7 for example, just add 7 and 8 which is 15, drop the 1, answer is 5.

When we work with a fixed number of digits, dropping the 1 (the overflow) is easy... and only requires a small adjustment to the way we find the inverse. Let's say we will stick to 4 digits always. That makes the inverse of 25 (actually 0025) become 9975. And the inverse of 0002 is 9998. Everything still works, 0050+9975=(1)0025, and 0007+9998=(1)0005.

We just have to remember that the normal human convention of not writing leading zeros and always expanding to the left as far as we need, isn't use.

A very nice video from Minute Physics that explains this.