Skip to content

Logic Gates

The three tiny decisions a computer can make, and the reason a processor with billions of parts contains nothing more complicated than these.

Class 6 to 8beginner25 min3 lessons1 interactive lab
By the end you will be able to
  • State what AND, OR and NOT do to their inputs
  • Complete a truth table for any of the five common gates
  • Explain why XOR answers a question AND and OR cannot
  • Describe what it means that NAND alone can build every other gate

Lesson 1 of 3

A switch that makes up its own mind

A plain switch does what your finger tells it. A logic gate is a small group of switches that picks its own output from the inputs, using a fixed rule every time. Bits go in, one bit comes out. The rule never changes.

Two lights wired in a row give you an AND gate: current flows only if both switches are closed. Wire them side by side and either path can complete the circuit on its own. That is an OR gate. Every other gate is a variation on how the switches connect.

Because the rule is fixed, the whole behaviour fits in a small table with four rows for two inputs. Open the lab and fill it in one row at a time. It feels like a puzzle, not a lecture.

Interactive lab

Logic gate bench

Pick a gate, feed it every possible input, and watch its truth table fill in one row at a time as the signals arrive.

Logic gates

AND · output is 1 only when both inputs are 1
0
A
0
B
AND
OUT
ABout
00?
01?
10?
11?
1 / 10

Start

A AND gate has two inputs and one output, and its whole behaviour is this: output is 1 only when both inputs are 1. We will feed it every possible combination and fill the table in as we go.

Continue

Lesson 2 of 3

AND, OR and NOT

AND outputs 1 only when every input is 1. It is the gate of strict requirements, and it is what a system uses when it checks that you are logged in AND allowed to see the page. OR outputs 1 when at least one input is 1, so it is the gate of alternatives: the alarm sounds if the door sensor OR the window sensor triggers.

NOT is the odd one out because it takes a single input and simply reverses it. On its own that sounds useless, but reversal is how you build a test for absence rather than presence, and it turns AND into NAND and OR into NOR, which is where the family of gates comes from.

Run the truth table for AND in the lab, then switch to OR without changing anything else. Only two of the four rows differ, and those two rows are the entire difference between requiring everything and accepting anything. Being able to point at exactly which rows differ is what it means to understand a gate.

Continue

Lesson 3 of 3

XOR, NAND, and why one gate can build them all

XOR outputs 1 when its inputs disagree. AND and OR cannot express that on their own, because both of them say 1 when the inputs are 1 and 1. Disagreement is exactly the case XOR cares about. This is the gate that adds numbers, which is the subject of the next topic.

NAND is AND with the answer flipped. Here is the surprising part: every other gate can be built from NAND gates alone. A NOT is a NAND with both inputs tied together. From there you can build AND, OR and XOR. Chip makers can perfect one tiny part and still build any circuit.

That is how a processor with billions of transistors stays understandable. It is not billions of different ideas. It is one simple idea, repeated an absurd number of times, wired in layers where each layer treats the one below as solved.

Continue

Worked examples

Read the code, then change it

Copy any example into the playground and break it on purpose. That is the fastest way to learn what each line is holding up.

Every gate, built and tested in the browserJavaScript
const AND = (a, b) => (a && b ? 1 : 0);
const OR = (a, b) => (a || b ? 1 : 0);
const NOT = (a) => (a ? 0 : 1);
const XOR = (a, b) => (a === b ? 0 : 1);
const NAND = (a, b) => NOT(AND(a, b));

// Proof that NAND is enough on its own.
const notFromNand = (a) => NAND(a, a);
const andFromNand = (a, b) => NAND(NAND(a, b), NAND(a, b));

for (const [a, b] of [[0, 0], [0, 1], [1, 0], [1, 1]]) {
  console.log(
    'a=' + a + ' b=' + b +
    '  AND=' + AND(a, b) +
    '  OR=' + OR(a, b) +
    '  XOR=' + XOR(a, b) +
    '  NAND=' + NAND(a, b) +
    '  AND-built-from-NAND=' + andFromNand(a, b),
  );
}

console.log('NOT from NAND:', notFromNand(0), notFromNand(1));

Practice

Work these out yourself

No answer key here on purpose: these are the questions worth thinking through before you move on. Open one and work it out.

0/3 attempted

Assessment

Check your understanding

Answer each question, then read the explanation. That is where the learning is.

0/5
  1. Question 1: An AND gate is given inputs 1 and 0. What does it output?
    Question 1 / 5

    An AND gate is given inputs 1 and 0. What does it output?

    Select an option first
  2. Question 2: Which gate outputs 1 exactly when its two inputs are different?
    Question 2 / 5

    Which gate outputs 1 exactly when its two inputs are different?

    Select an option first
  3. Question 3: Why is NAND described as a universal gate?
    Question 3 / 5

    Why is NAND described as a universal gate?

    Select an option first
  4. Question 4: A two-input gate's behaviour is fully described by how many rows?
    Question 4 / 5

    A two-input gate's behaviour is fully described by how many rows?

    Select an option first
  5. Question 5: An alarm should sound if the door sensor triggers or the window sensor triggers. Which gate is that?
    Question 5 / 5

    An alarm should sound if the door sensor triggers or the window sensor triggers. Which gate is that?

    Select an option first
5 of 5 questions left.

Finished this topic?

0 of 3 lessons marked done. Marking the topic complete ticks the rest.