# Logic gates

by Chris Woodford. Last updated: July 11, 2021.

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."

### Contents

## What are 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:

### AND

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.

### OR

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:

### NOT

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.