Logic gates and truth tables explained

What each logic gate does, how to fill in a truth table in binary order, and how a safety rule written in English becomes a circuit. Four demos run on the page, two of them with the robot's own sensors as the inputs and the gate's output charted as it drives.

Guidefree, runs in your browser

Inside a processor there are billions of tiny switches. Each one is either on or off, and a group of them wired together makes a decision: if this and that, then turn this other one on. A group like that is a logic gate, and everything a computer does is built out of them.

A gate takes one or two inputs that are 1 or 0, and gives one output that is 1 or 0. A truth table lists the output for every possible set of inputs, so it says exactly what the gate does with no words left over. This page explains the six gates an exam asks for, how to fill in a truth table, and how a safety rule written in English becomes a circuit. Each demo is a real program on a simulated BugBot, and in two of them the robot's own sensors are the inputs.

1 and 0 are just high and low

In a circuit, 1 and 0 are two voltages. On the BugBot's board, 1 is about 3.3 volts and 0 is about 0 volts. Nothing in between is allowed to matter, which is what makes the signal easy to keep clean and easy to copy.

Anything that is either true or false can be one of those bits:

  • a switch pressed or not pressed;
  • the battery above 20 percent or not;
  • the distance sensor reading less than 30 cm or not.

Turning a measurement into a bit like that is how a rule about the world becomes a rule about circuits.

The six gates

Gate Output is 1 when Inputs Also written
AND both inputs are 1 2 A · B, A ∧ B
OR at least one input is 1 2 A + B, A ∨ B
NOT the input is 0 1 Ā, ¬A
XOR the inputs are different 2 A ⊕ B, A ⊻ B
NAND the inputs are not both 1 2 the bar over A · B
NOR neither input is 1 2 the bar over A + B

NOT is the only one with a single input. NAND is an AND with a NOT on the end, and NOR is an OR with a NOT on the end, which is why their symbols are the AND and OR symbols with a small circle added. That circle always means "and then invert".

The six logic gates with their symbols and truth tables: AND, OR, NOT, XOR, NAND and NORANDABQ000010100111ORABQ000011101111NOTAQ0110XORABQ000011101110NANDABQ001011101110NORABQ001010100110
Each gate's shape says what it does, and the small circle on the output of NOT, NAND and NOR means the answer is inverted. The Q column is the output for every combination of inputs, listed in binary order.

OCR writes NOT as ¬, AND as ∧, OR as ∨ and XOR as ⊻. AQA writes AND as a dot, OR as a plus and NOT as a bar over the letters, and how far the bar reaches decides where the NOT goes. Learn the set your board uses, and read the other set when you meet it.

This program has the six gates as functions, prints the whole table, and charts the one you pick.

The table has four rows, one for each pair of inputs. The chart shows A, B and the gate you picked: XOR goes to 1 on the two rows where A and B are different.
The program
from bugbot import *
connect()

# change this and press Run: AND, OR, XOR, NAND or NOR
GATE = "XOR"

def AND(a, b):
    return 1 if a == 1 and b == 1 else 0

def OR(a, b):
    return 1 if a == 1 or b == 1 else 0

def NOT(a):
    return 1 - a

def XOR(a, b):
    return 1 if a != b else 0

def NAND(a, b):
    return NOT(AND(a, b))

def NOR(a, b):
    return NOT(OR(a, b))

gates = {"AND": AND, "OR": OR, "XOR": XOR, "NAND": NAND, "NOR": NOR}

print("A B | AND OR XOR NAND NOR | NOT A")
for a in [0, 1]:                      # the outer input changes slowest
    for b in [0, 1]:
        print(a, b, " |  ", AND(a, b), " ", OR(a, b), " ", XOR(a, b),
              "  ", NAND(a, b), "  ", NOR(a, b), " |  ", NOT(a))
        plot("A", a)
        plot("B", b)
        plot(GATE, gates[GATE](a, b))
        wait(0.5)
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Each gate is a function: bits in, one bit out. The two loops go through the input pairs in binary order: 00, 01, 10, 11. That is the order an exam expects, and the easiest way to get it is to count up in binary, with the last input changing on every row and the first changing halfway down.

Filling in a truth table

A truth table has one column for each input, one row for each combination of them, and a column for the output. With n inputs there are 2n rows, because every extra input doubles the number of combinations: 2 inputs give 4 rows, 3 give 8, 4 give 16.

For an expression with more than one operator, give yourself a working column for each step, and do the innermost bracket first:

A B NOT B A AND NOT B
0 0 1 0
0 1 0 0
1 0 1 1
1 1 0 0

Mark the working columns as working and the last one as the answer. Examiners give marks for the columns, so a wrong final answer with the right working still scores.

Two inputs from the robot's own eyes

Gates are more interesting when the inputs come from somewhere real. The BugBot's depth sensor reads a fan of eight distances across the 45 degrees in front of it. This program splits the fan in half and turns each half into a bit: A is "something near on the left", B is "something near on the right". Then it feeds both into the gate you pick while the robot turns on the spot between four boxes.

As the robot turns, XOR was 1 on 29 of the 60 readings: the times a box was near on one side and not the other. AND was 1 on 13, and NOR on 18.
The program
from bugbot import *
connect()

# change this and press Run: AND, OR, XOR, NAND or NOR
GATE = "XOR"
NEAR = 35        # a side counts as blocked under this many cm

def AND(a, b):
    return 1 if a == 1 and b == 1 else 0

def OR(a, b):
    return 1 if a == 1 or b == 1 else 0

def NOT(a):
    return 1 - a

def XOR(a, b):
    return 1 if a != b else 0

gates = {"AND": AND, "OR": OR, "XOR": XOR,
         "NAND": lambda a, b: NOT(AND(a, b)),
         "NOR": lambda a, b: NOT(OR(a, b))}

count = 0
drive(0, 0, 50)                       # turn on the spot
for i in range(60):                   # 15 seconds
    view = scan()
    left = min(d for angle, d in view if angle < 0)
    right = min(d for angle, d in view if angle > 0)
    a = 1 if left < NEAR else 0       # A: something near on the left
    b = 1 if right < NEAR else 0      # B: something near on the right
    q = gates[GATE](a, b)
    count = count + q
    plot("A: left blocked", a)
    plot("B: right blocked", b)
    plot(GATE, q)
    wait(0.25)
stop()
print(GATE, "was 1 on", count, "of the 60 readings")
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Each gate now means something you can say out loud:

  • AND is "a box straight ahead", because it is near on both sides at once. It was true on 13 readings.
  • OR is "a box anywhere in view", true on 42.
  • XOR is "a box on one side only", true on 29. That is the useful one for getting past something, because it says which way is free.
  • NOR is "nothing near at all", true on 18, and that is when it is safe to drive.

Change GATE and run it again. Change NEAR to 25 and a box has to be closer before it counts as blocked: AND falls to 1 reading and XOR to 11, while NOR, which is the one that asks for nothing in the way, rises to 48.

Turning a rule in words into a circuit

This is the exam skill, and it is four steps.

  1. Name the inputs. Each one has to be a thing that is true or false. "The way ahead is close" is a bit. "The distance" is not.
  2. Write the rule with AND, OR and NOT. Keep the words in the same order as the sentence where you can.
  3. Draw one gate for each operator. Work out which operator is applied last, because its gate makes the output and everything else feeds into it.
  4. Check it with a truth table. Every row, not just the ones you expect.

Take this rule: sound the alarm when the robot is close to something and still moving. Two inputs, one operator:

A = close      the depth sensor reads less than 30 cm
B = moving     the robot is going faster than 2 cm a second

alarm = A AND B
go    = NOT A
The rule 'sound the alarm when the robot is close and still moving' as two gates, with a truth table of how often each row came up in the demosound the alarm when the robot is close to something and still movingA = close: the depth sensor reads under 30 cmB = moving: the robot is going faster than 2 cm a secondABalarmgoalarm = A AND Bgo = NOT Ain the demo's 70 readingsABalarmreadings000101042100811119the alarm sounded on 19 of the 70 readings
One gate for each operator in the sentence. The AND makes the output, so it is the gate nearest the alarm. In the demo's run the robot was close but had already stopped on 8 readings, and on those the AND held the alarm off.

The program below is that circuit, with the two gates written as functions. The robot drives forwards while go is 1, backs off while it is 0, and the piezo and the LED follow alarm.

The alarm is on for 19 of the 70 readings: only when close and moving are both 1. There are readings where the robot is close but has stopped, and the AND holds the alarm off.
The program
from bugbot import *
connect()

# change these and press Run
STOP_AT = 30      # closer than this counts as close, in cm
MOVING = 2        # faster than this counts as moving, in cm/s

def AND(a, b):
    return 1 if a == 1 and b == 1 else 0

def NOT(a):
    return 1 - a

alarms = 0
for i in range(70):                   # 14 seconds
    vx, vy = velocity()
    close = 1 if distance() < STOP_AT else 0
    moving = 1 if abs(vy) > MOVING else 0
    go = NOT(close)                   # the way ahead is clear
    alarm = AND(close, moving)        # close, and still moving
    plot("A: close", close)
    plot("B: moving", moving)
    plot("alarm", alarm)
    if alarm == 1:
        led("red")
        alarms = alarms + 1
    else:
        led("green")
    if go == 1:
        drive(60, 0)                  # forward
    else:
        drive(-40, 0)                 # back off
    wait(0.2)
stop()
print("the alarm was on for", alarms, "of the 70 readings")
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Look at the chart. The "alarm" line is 1 only where both of the lines above it are 1. Near the end of each approach there are readings where "close" is 1 but the robot has already stopped, so "moving" is 0 and the alarm stays quiet. That is the AND gate doing the job the sentence asked for. A circuit like this one can be built with no processor at all, which is why safety interlocks on real machines often are.

Raise MOVING to 8 and the alarm sounds on 1 reading out of the 70, because by the time the robot is close it has already slowed to under 8 cm a second. That is a test you can do on the page: change one input's meaning and watch the output column change.

Three inputs, eight rows, and the rows that never happen

Add a third input and the table doubles to eight rows. This program takes three bits from the depth sensor, left, ahead and right, and works out clear, which is 1 only when all three are 0. Written as one gate that is a three-input NOR. The robot drives while it is clear and turns while it is not, and the program counts how often each of the eight rows actually came up.

The chart shows which row of the truth table the robot was in, 0 to 7. Row 000, everything clear, came up 43 times out of 70. Row 101, blocked on both sides with a gap straight ahead, never came up at all.
The program
from bugbot import *
connect()

# change this and press Run
NEAR = 35        # a direction counts as blocked under this many cm

def OR(a, b):
    return 1 if a == 1 or b == 1 else 0

def NOT(a):
    return 1 - a

seen = [0, 0, 0, 0, 0, 0, 0, 0]
for i in range(70):                   # 14 seconds
    view = scan()
    left = min(d for angle, d in view if angle < -10)
    right = min(d for angle, d in view if angle > 10)
    a = 1 if left < NEAR else 0
    b = 1 if distance() < NEAR else 0
    c = 1 if right < NEAR else 0
    clear = NOT(OR(OR(a, b), c))      # a three-input NOR
    row = 4 * a + 2 * b + c           # which row of the truth table this is
    seen[row] = seen[row] + 1
    plot("row", row)
    plot("clear", clear)
    if clear == 1:
        drive(50, 0)                  # nothing near: go
    else:
        drive(0, 0, 60)               # something near: turn
    wait(0.2)
stop()
for row in range(8):
    print("row", format(row, "03b"), "happened", seen[row], "times")
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

The truth table of the three-input NOR, with a bar for how many of the demo's 70 readings landed on each rowclear = NOT (left OR ahead OR right)rowleftaheadrightclearreadings0000001430010010401001003011011041001000910110100never happened110110061111110143 of the 70 readings were row 000, the only row where the robot drove forwards.
Three inputs make 2 to the power 3 = 8 rows, and the output is 1 on one of them. The bars are how often each row came up while the robot drove. Row 101 never came up, and the circuit still has to answer for it.

Two things worth taking from the printout. The first is that 4 * a + 2 * b + c turns the three bits into the row number, 0 to 7, which is the same as reading the inputs as a binary number: that is why binary order is the natural order for a truth table. The second is that one row came up zero times. A truth table still has to give an answer for it, because a circuit has no way of knowing that a reading is unlikely. Real hardware faults are exactly the rows nobody expected.

The three-input NOR here is built from two OR gates and a NOT. Chips do sell three-input gates, but any of them can be made from two-input ones this way.

NAND and NOR build everything else

NAND and NOR have a property the others do not: every gate can be built from copies of just one of them. Tie a NAND's two inputs together and it behaves as a NOT. Put that NOT after another NAND and you have an AND back again. Keep going and you can build OR and XOR too.

This matters in a factory. A chip made of one kind of gate, repeated, is simpler to design and cheaper to make than one that mixes gate types, so a lot of real logic inside a processor is NAND all the way down. The lesson Logic gates and notation has the task of building the other gates from NAND alone.

There is also a pair of rules for moving a NOT through a gate, called De Morgan's laws: NOT (A AND B) is the same as (NOT A) OR (NOT B), and NOT (A OR B) is the same as (NOT A) AND (NOT B). They are how a circuit gets rewritten into the gates you have to hand, and De Morgan's laws works through them.

Mistakes that lose marks

  • Rows out of order or missing. Count up in binary: 00, 01, 10, 11. Three inputs need all eight rows.
  • Mixing up XOR and OR. OR is 1 when both inputs are 1. XOR is 0 there.
  • Mixing up NAND and "both are 0". NAND is 1 on three rows out of four. NOR is the one that is 1 only when both are 0.
  • Doing the operators left to right. AND is worked out before OR, the way multiply comes before add. A ∨ B ∧ C means A ∨ (B ∧ C).
  • Missing where the bar reaches. Two short bars are NOT on each input. One long bar is NOT on the whole expression, which is a different circuit.
  • Forgetting the inversion circle. An AND symbol with a circle on its output is a NAND, and its output column is upside down compared with AND.
  • No working columns. Give each step its own column. Marks are there for the steps.

Where this is taught

Questions

What is a logic gate?

A small circuit that takes one or two inputs, each a 1 or a 0, and gives one output that is a 1 or a 0. The output depends only on the inputs, and a truth table lists it for every combination of them. Processors are built from billions of gates.

What are the six logic gates?

AND, OR, NOT, XOR, NAND and NOR. AND is 1 when both inputs are 1. OR is 1 when at least one is. NOT flips its single input. XOR is 1 when the inputs are different. NAND is the opposite of AND, and NOR is the opposite of OR.

What is a truth table?

A table with a column for each input and one row for every combination of them, plus a column for the output. It defines what a gate or a circuit does, with no room for argument. Rows are listed in binary order.

How many rows does a truth table have?

2n, where n is the number of inputs. Two inputs give 4 rows, three give 8, four give 16, and eight inputs give 256.

What is the difference between OR and XOR?

They only differ on the last row. OR is 1 when at least one input is 1, including when both are. XOR is 1 when exactly one input is 1, so it is 0 when both are. XOR is the "one or the other, and not both" gate.

What is the difference between NAND and NOR?

NAND is 1 unless both inputs are 1, so it is 1 on three of the four rows. NOR is 1 only when both inputs are 0, so it is 1 on one row. They are the outputs of AND and OR with a NOT on the end.

Why are NAND and NOR called universal gates?

Because any other gate can be built from copies of either one. Tie a NAND's inputs together and it is a NOT; follow another NAND with that NOT and it is an AND. Chips are cheaper to make from one repeated kind of gate.

How do you convert a sentence into a Boolean expression?

Name each condition as an input that is true or false, then join them with AND for "and", OR for "or" and NOT for "not". Work out which operator applies last, because that one makes the output. Then check every row of the truth table against the sentence.

What does the small circle on a gate symbol mean?

Inversion. A circle on the output turns AND into NAND and OR into NOR. A circle on an input means that input is inverted before the gate uses it, which saves drawing a separate NOT gate.

Are logic gates on the GCSE and A level specifications?

Yes, on both. At GCSE, OCR asks for AND, OR and NOT and circuits of up to three inputs, AQA adds XOR, and Edexcel asks you to apply AND, OR and NOT in truth tables. At A level, AQA asks for all six gates and their symbols and for simplifying with Boolean identities and De Morgan's laws, OCR covers logic diagrams and Boolean algebra including De Morgan's laws and Karnaugh maps, and Eduqas asks you to use the operators in truth tables.

Learn it step by step

These lessons build the same ideas one at a time, each with tasks the simulator marks.

  1. F2.3 else, elif and Boolean operators Decisions and loops, GCSE
  2. F9.1 Logic gates and truth tables Logic and computer systems, GCSE
  3. F9.2 Logic circuits and expressions Logic and computer systems, GCSE
  4. A8.1 Logic gates and notation Boolean algebra and logic circuits, A level
  5. A8.2 Circuits, expressions and truth tables Boolean algebra and logic circuits, A level
  6. A8.3 Boolean identities and laws Boolean algebra and logic circuits, A level
  7. A8.4 De Morgan's laws Boolean algebra and logic circuits, A level
  8. A8.5 Simplifying expressions Boolean algebra and logic circuits, A level
  9. A8.6 Karnaugh maps Boolean algebra and logic circuits, A level
  10. A8.7 Half adders and full adders Boolean algebra and logic circuits, A level
  11. A8.9 Project: the robot's safety logic Boolean algebra and logic circuits, A level
Open the lessons