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!

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

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:

- http://ozark.hendrix.edu/~burch/cs/230/reading/09/3.html Flags, incrementers and ALU

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.

file: /Techref/logic/sub5.htm, 6KB, , updated: 2015/5/4 16:22, local time: 2024/8/11 02:56, |

©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?<A HREF="http://www.piclist.com/techref/logic/sub5.htm"> Digital Logic Tutorial, Binary Subtractor</A> |

Did you find what you needed? |

PICList 2024 contributors:
o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr. |

## Welcome to www.piclist.com! |

.