Skip to main content

Level 0: How computers work · 12 min

Logic gates: switches that decide

Goal: know what the AND, OR, NOT and XOR gates do.
You need: Binary.

The problem

We can now hold numbers as bits. But a computer must also do things with them, like compare and add.

One switch cannot decide anything. But several switches connected together can follow a rule.

What is a logic gate?

A logic gate is a small circuit that takes one or two bits as input and gives one bit as output. It always follows the same rule. It is built from transistors.

Two switches, two rules

Put two switches one after the other on the same path. Current gets through only if the first and the second are closed.

Put them side by side, on two separate paths to the bulb. Current gets through if the first or the second is closed.

These are the ideas behind the first two gates.

AND: both must be yes

A cash machine gives money only if your card is in and your PIN is correct.

Card in?PIN correct?Money?
no (0)no (0)no (0)
no (0)yes (1)no (0)
yes (1)no (0)no (0)
yes (1)yes (1)yes (1)

AND gives 1 only when both inputs are 1.

OR: at least one is yes

A house with two doorbell buttons. The bell rings if someone presses the front button or the back button.

Front pressed?Back pressed?Rings?
no (0)no (0)no (0)
no (0)yes (1)yes (1)
yes (1)no (0)yes (1)
yes (1)yes (1)yes (1)

OR gives 1 when at least one input is 1.

NOT: the opposite

A fridge light is on when the door is not closed.

Door closed?Light on?
yes (1)no (0)
no (0)yes (1)

NOT has only one input. It flips the bit.

XOR: one, but not both

A staircase lamp often has two switches, one downstairs and one upstairs. Start with both down and the lamp off. Flip either one and the lamp changes. Here 1 means "this switch is flipped up".

DownstairsUpstairsLamp on?
00no (0)
01yes (1)
10yes (1)
11no (0)

XOR gives 1 when the two inputs are different. The name is short for "exclusive OR": one or the other, but not both.

Try it

Choose a gate. Before you change an input, say what the output will be. Then check.

1 only when both inputs are 1.

Input A0
Input B0

Output 0

ABAND
000
010
100
111

Truth tables

The table under the demo is a truth table. The small tables in the examples above were truth tables too. It lists every possible input and the output for each one. Two inputs have only 4 combinations, so we can check them all.

ABANDORXOR
00000
01011
10011
11110

What comes next

Look at the AND and XOR columns again. In the next lesson, these two gates work together to add numbers.

Check yourself

1. An AND gate gets A = 1 and B = 0. What is the output?

2. Which gate gives 1 for the inputs 1 and 1?

3. What does a NOT gate do with 0?

Practice

An alarm must ring when the system is armed and at least one of two things happens: the door opens or the window opens. Which two gates do you need, and how do you connect them?

Show answer

First put door and window into an OR gate. Then put that result and "armed" into an AND gate. alarm = armed AND (door OR window)

Recap

  • A logic gate takes bits in and gives one bit out, using a fixed rule.
  • AND: both. OR: at least one. NOT: the opposite. XOR: different.
  • A truth table lists every input and its output.
  • Gates connected together can do arithmetic.