by Chris Woodford. Last updated: October 2, 2013.
You probably know that calculators and computers store decimal (0-9) numbers as long strings of zeros and ones in a form called binary code. Each number is stored using microscopic electronic switches called transistors. It's easy to store binary numbers simply by switching transistors on and off. Switching on a transistor stores a number one; switching it off stores a zero. So storing numbers is easy. But how can you add, subtract, multiply, and divide using nothing but electric currents? Calculators and computers do this using clever electronic circuits called logic gates. Let's take a closer look at what they are and how they work.
Photo: A gate can keep you out or let you into a field. In the same way, a microscopic logic gate is a barrier in an electronic circuit that can let electricity through or stop it flowing altogether. Put lots of logic gates together and you make a machine that's capable of basic mathematical "reasoning."
Logic gates: circuits that compare
A logic gate might sound horribly complex, but it's simply an electric circuit with two inputs and an output. It receives two incoming electric currents, compares them, and sends on a new, outgoing electric current depending on what it finds. A logic gate is a bit like a doorman or bouncer who is allowed to let people into a nightclub only if they pass certain tests. There are quite a few different types of logic gate, the most common of which are called AND, OR, NOT, XOR (Exclusive Or), NAND (NOT AND), and NOR (NOT OR). Let's look at the three simpler ones, AND, OR, and NOT:
Suppose you go to a nightclub where the doorman's job is to enforce a simple rule: "Everyone in your group must wear a tie to come in". You go along with a friend one night. If you're both wearing ties, you'll get in. If only one of you is wearing a tie, or if neither of you is, neither of you will get in. An AND logic gate works the same way with two electrical inputs. If both inputs are switched on (that is, carry a number 1), the output will be 1 as well. Otherwise the output will be 0. In electronics, we can represent an AND gate with this little symbol. Three ways in which the gate can work are shown below.
You're not wearing a tie, so you go to another club further down the street. Here, the person on the door is enforcing a different rule: "A group of people can come in if any one of them is a member". If either you or your friend is a member, or if you both are members, you can both come in. If neither of you is a member, you're both left out in the cold. An OR logic gate works this way with two electrical inputs. If either input is switched on (that is, carries a number 1), the output will be 1 as well. Otherwise the output will be 0. In electronics, we represent an OR gate with a different symbol. Three ways in which it can work are shown beneath:
So far, you've failed to get into either of the clubs. But there's one last hope: you know a friend is having a party a few streets away. The only trouble is, there's a really argumentative and contrary person on the door. He talks to each person in turn as they approach him. If you're nice and polite, he shouts abuse at you and turns you away. But if you're rude to him, he likes that for some reason and lets you in. In other words, he does exactly the opposite of what you'd expect! In electronics, there's a logic gate that works in the same, contrary way and it's called a NOT gate or inverter. Unlike AND and OR gates, it has only one input and one output. The output is exactly the opposite of the input, so if the input is a 0, the output is a 1 and vice versa. Here's how we represent a NOT in electronics. Two ways it can work are shown beneath.
The other three common logic gates are variations on these three. XOR (Exclusive OR) is like an OR, but it switches off if both the inputs are switched on. NAND is just like AND, only the end result is swapped over (so where AND produces an output of 1, NAND produces an output of 0). NOR is like OR with the end result swapped over in the same way.
This logical way of comparing numbers to make decisions that produce either a yes or no, 1 or 0, true or false is called Boolean algebra after its discoverer, English mathematician George Boole (1815–1864), who set out the idea in an 1854 book titled An Investigation of the Laws of Thought, on Which Are Founded the Mathematical Theories of Logic and Probabilities. His objective was to show how complex human reasoning could be represented in a logical, mathematical form.
Controlling a calculator display with logic gates
Calculators can do all the things they need to do using different combinations of logic gates. It's logic gates that control how the display works in a calculator and more logic gates that figure out the results of calculations.
For example, each segment of a calculator's display is switched on and off by a series of logic gates that are connected together. Consider just the bottom lower right segment (shown in the picture, right, in blue). We need to turn this segment on if we're showing the numbers 0 (binary 00), 1 (01), 3 (11), 4 (100), 5 (101), 6 (110), 7 (111), 8 (1000), and 9 (1001)—but not if we're showing the number 2 (10). We can make the segment switch on and off correctly for the numbers 1–10 by rigging up three OR gates and one NOT gate like this.
If we feed the patterns of binary numbers into the four inputs on the left, the segment will turn on and off correctly for each one. For example, feed in the number 7 as the four inputs 0111 and the gates will trigger like this, correctly switching on the segment:
The other six segments of each digit are controlled by other combinations of logic gates.
Calculating with logic gates
It's possible to do any calculation you can think of by combining a different pattern of logic gates. The gates compare and combine two series of binary numbers, corresponding to the decimal numbers in your calculation, and produce a new series of binary numbers equal to the result. Addition is carried out by circuits called half-adders and full adders (which can carry on the results of a previous addition); subtractors work in a very similar way; multipliers use a collection of AND gates to multiply two numbers with a series of additions.
Find out more
On this website
On other sites
- How logic gates add 2+3: How does a calculator add two binary numbers? This basic diagram shows you how four chips (made from AND, OR, and NOT gates) do the job.
- Adder (electronics): This more detailed (and less clear) article from Wikipedia explains how different logic gates can be used to add numbers in circuits called half adders, full adders, and more complex variations such as ripple-carry adders.
Books for younger readers
- Computer Coding for Kids by Carol Vorderman. Dorling Kindersley, 2014. A fun overview of computer programming for young readers, including logic gates (covered on p186).
- Eyewitness: Electronics by Roger Bridgman. Dorling Kindersley, 2007. A simple introduction designed for ages 9–12.
- Electronic Circuits for the Evil Genius by Dave Cutcher. McGraw-Hill, 2005. Although not exclusively for young readers, the Evil Genius format is sure to appeal to teenagers. This book is a good hands-on introduction to digital logic and the projects include a variety of logic gate circuits.
Books for older readers
- Ones and Zeroes: Understanding Boolean algebra, Digital Circuits, and the Logic of Sets by John Gregg. IEEE, 1998. A good introduction that explains Boolean algebra in the context of modern digital electronics and computing. Includes practical exercises
- Boolean Algebra and Its Applications by J. Eldon Whitesitt. Courier Dover, 1995. A reprinted book from 1961, this explains Boolean algebra with a mathematical approach based on sets, Venn diagrams, and so on. Unfortunately, it pre-dates modern computing so there's no coverage of what we now call digital logic.
- Fundamentals of Digital Logic and Microcomputer Design by Mohamed Rafiquzzaman. John Wiley & Sons, 2005. Chapter 3 covers logic gates.
- Digital Fundamentals by Thomas L. Floyd. Prentice Hall, 2003. A clear introduction to digital circuits, including counters, logic gates, memory and storage, signal processing, and related topics.
- A Practical Introduction to Computer Architecture by Daniel Page. Springer, 2009. A more comprehensive guide covering all aspects of computer internals. Quite a lot of math.