Watch on Instagram

Circuit Bytes · Episode 49 · Electronics · Logic gates

"Logic gates: AND, OR, NOT"

A logic gate takes in bits and puts out one bit by a fixed rule. AND gives 1 only when both inputs are 1 (switches in a row), OR gives 1 when either input is 1 (switches side by side), and NOT flips its one input. Millions of them wired together add, compare and run every app.

Posted · About a 1 minute watch, 4 minute read · Take the quiz

What a logic gate does

Last episode showed that inside a chip, every bit is a voltage: low means 0 and high means 1. A logic gate is a tiny circuit that looks at its input bits and sets its output bit by one fixed rule. The rule is written as a truth table: a list of every possible input and the output it gives.

A handy way to picture a gate is with switches and a lamp. A switch that is on (closed) stands for a 1, and a lit lamp stands for an output of 1.

AND: switches in a row

Put two switches in series, one after the other, between a battery and a lamp. Current can only reach the lamp if it gets through both switches, so the lamp lights only when both are on. That is the AND rule:

| A | B | out | |---|---|---| | 0 | 0 | 0 | | 0 | 1 | 0 | | 1 | 0 | 0 | | 1 | 1 | 1 |

1 and 1 gives 1; anything else gives 0.

OR: switches side by side

Now put the two switches in parallel, side by side. Current has two paths to the lamp, so either switch is enough to light it. That is the OR rule: the output is 1 if A or B (or both) is 1. The only way to get 0 out is 0 and 0 in.

| A | B | out | |---|---|---| | 0 | 0 | 0 | | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 1 |

NOT: the flipper

NOT, also called an inverter, has just one input, and its output is always the opposite: 0 becomes 1, and 1 becomes 0. Its symbol is a triangle with a small circle (the "bubble") at its tip; that bubble means "flip it" wherever it appears on a gate symbol.

From three gates to a computer

One gate is simple, but gates can feed into other gates. A few of them together can add two bits; a few dozen can add whole numbers or compare them. A modern phone chip has billions of transistors forming huge numbers of gates, and everything it does, from math to every app, comes down to these simple rules running very fast.

Try it yourself

Follow an AND gate into a NOT gate. Feed the AND gate 1 and 1: AND gives 1, and NOT flips it to 0. Now try 1 and 0: AND gives 0, and NOT flips it to 1. Make a little truth table for all four inputs. This combination has its own name, NAND ("not AND"), and it turns out every other gate can be built from NAND gates alone.

Good to know

  • The switches and lamp are a teaching model. Real chips build gates from transistors acting as switches; the next episode shows how.
  • "Switch on = 1, lamp lit = 1" is one convention; some circuits use active-low signals, where low voltage means "on".
  • AND and OR gates can have more than two inputs: a 3-input AND gives 1 only when all three are 1, and a 3-input OR gives 1 when any input is 1.
  • OR here is "inclusive OR": 1 and 1 also gives 1. The gate that gives 1 only when the inputs differ is called XOR.
  • Battery or low-voltage supplies only; leave wall wiring to a licensed electrician.

Answer to the comment question

Feed a 1 and a 0 into an OR gate, then send its output through a NOT gate. What comes out? 0. The OR gate sees a 1 on one input, so its output is 1 (either one on is enough). The NOT gate then flips that 1 into a 0. (OR followed by NOT is called a NOR gate.)

Test your knowledge

Quick quiz: "Logic gates: AND, OR, NOT"

4 multiple-choice questions. Pick one answer for each, then check how you did.

1When does a two-input AND gate output a 1?
2In the switch-and-lamp picture, which layout acts like an OR gate?
3Which inputs make an OR gate output a 0?
4What does a NOT gate do?

Sources

Video transcript

How can three simple gates build a whole computer?

A logic gate takes in bits and puts out one bit, following one rule. Picture switches and a lamp.

AND is two switches in a row. The lamp lights only if both are on. One and one gives one; anything else, zero.

OR is two switches side by side. Either one lights the lamp. Only zero and zero gives zero.

NOT has one input and flips it. Zero becomes one, and one becomes zero.

Wire millions together and they add numbers, compare them, and run every app on your phone.

Feed a one and a zero into an OR gate, then send its output through a NOT gate. What comes out? Comment your answer below!

That's your byte.

More from Circuit Bytes

All Circuit Bytes lessons