Year 9 · Physical Computing · Part B: Inside the machine

Logic gates

In one line

A logic gate is a tiny circuit. Bits go in, one bit comes out, by a fixed rule. Join millions of gates and a CPU can decide things and do maths.

1

Know the rules for NOT, AND, OR and XOR, and draw their symbols.

2

Write a full truth table for any circuit, with no missing rows.

3

Turn a circuit into an expression, and an expression into a circuit, in AQA or OCR notation.

4

Explain how a half adder uses XOR and AND to add two bits.

5

Link gates to real chips, transistors and and / or / not in Python.

BooleanA value that is only True or False: 1 or 0.
Logic gateA circuit with one fixed rule: bits in, one bit out.
Truth tableEvery input pattern, and the output for each.
Boolean expressionA gate circuit written as a line of text: Q = A AND B.
Half adderXOR + AND: adds two bits.
TransistorAn electronic switch. Gates are built from them.

Boolean: true or false

Some questions only have two answers. Is the door open? Is the button pressed? Is the score over 50? The answer is true or false. Nothing in between.

  1. George Boole (1815–1864) wrote maths for true/false statements. His book The Laws of Thought came out in 1854. Values that are only true or false are called Boolean in his honour.

  2. In 1937, Claude Shannon noticed that Boole's maths fits electrical switches perfectly. Switch on = true = 1. Switch off = false = 0.

  3. So a circuit of switches can make decisions. How you join the switches decides the rule. Try it below.

▶

Two ways to join two switches

current flowing (1)no current (0)
?Why?

Why does the left one need both? The switches are in one line (series). Current must pass through A and then B. One gap anywhere stops it. On the right, there are two paths (parallel). Current can take path A or path B.

★Remember

1 = on = true. 0 = off = false. Series switches make AND. Parallel switches make OR.

The three basic gates: NOT, AND, OR

Engineers don't draw switches every time. Each rule gets its own symbol. Inputs come in on the left. The output, called Q, leaves on the right.

These are the ANSI symbols used by AQA and OCR. Learn the shapes: the flat back of AND, the curved back of OR, the bubble on NOT.
ANOT A
01
10
ABA AND BA OR B
0000
0101
1001
1111
  1. NOT has one input. It flips it: 0 → 1, 1 → 0. It is also called an inverter.

  2. AND gives 1 only if both inputs are 1. One 0 is enough to make it 0.

  3. OR gives 1 if at least one input is 1. It is only 0 when both are 0.

e.g.Example

"You can go out if your homework is done AND your room is tidy." Both must be true. "Juice OR milk with your breakfast?" Having both still counts as true.

XOR, plus NAND and NOR

XOR means "exclusive OR": one or the other, but not both. It gives 1 when the inputs are different.

The bubble always means "flip it". NAND = AND then NOT. NOR = OR then NOT.
ABA XOR BA NAND BA NOR B
00011
01110
10110
11000
e.g.Example

A landing light with a switch at the top and the bottom of the stairs. Flip either switch and the light changes. The light is on when the two switches are in different positions. That's XOR.

★Exams

AQA 8525: NOT, AND, OR and XOR. The spec says students do not need NAND or NOR. OCR J277: only NOT, AND and OR. NAND and NOR are an extension here: useful later, because every other gate can be built from NAND alone.

?Why?

Why bother with NAND? A NAND gate is the cheapest gate to build from transistors (4 of them). Flash memory, like a USB stick, is even called "NAND flash" after it.

Truth tables, with no missing rows

A truth table lists every pattern of inputs, one per row, and the output for each. The trick to never missing a row: count in binary.

Read each row left to right: it is the row number written in binary. A is the 4s column, B the 2s column, C the 1s column.
  1. Count the inputs: n.

  2. Each input is 0 or 1: 2 choices. So there are 2 × 2 × … = 2ⁿ rows.

  3. Write the row numbers 0 up to 2ⁿ − 1 in binary, with n digits. Those are your input columns.

  4. Quick check: the rightmost input column goes 0, 1, 0, 1. The next one goes 0, 0, 1, 1. Each column to the left doubles the run.

By hand: 3 inputs

  1. n = 3 inputs: A, B and C.

  2. R = 2³ = 2 × 2 × 2 = 8 rows.

  3. Count from 0 to 8 − 1 = 7 in 3-bit binary: 000, 001, 010, 011, 100, 101, 110, 111.

  4. Check: 4 inputs would need 2⁴ = 16 rows. One more input doubles the table.

▶

Fill in the truth table

The inputs are done for you. Tap each output cell to set it to 0 or 1. Then press Check.

Expression:

Gates as arithmetic

Because the inputs are only 0 or 1, each gate is a tiny sum. This is a second way to remember the rules, and it shows why gates can do maths.

ABAND
A × B
OR
A + B − A × B
XOR
(A + B) mod 2
000 × 0 = 00 + 0 − 0 = 00 mod 2 = 0
010 × 1 = 00 + 1 − 0 = 11 mod 2 = 1
101 × 0 = 01 + 0 − 0 = 11 mod 2 = 1
111 × 1 = 11 + 1 − 1 = 12 mod 2 = 0

By hand: A = 1, B = 1

  1. AND: Q = A × B = 1 × 1 = 1. Multiplying by 0 always gives 0, so one 0 is enough to make AND give 0.

  2. OR: start with A + B = 1 + 1 = 2. That's too big: an output can only be 0 or 1.

  3. Take away the "both are 1" case once: 2 − (1 × 1) = 2 − 1 = 1.

  4. XOR: A + B = 2. "mod 2" means the remainder after dividing by 2. 2 ÷ 2 = 1 remainder 0. So Q = 0.

  5. NOT A: Q = 1 − A = 1 − 1 = 0.

  6. Look again at XOR: 1 + 1 = 2, and 2 in binary is 10. XOR kept the last digit (0). AND gave the carry (1). That is how a half adder works (Step 8).

Boolean expressions and notation

A Boolean expression writes a circuit as one line of text, like Q = A AND B. Different books use different symbols for the same idea.

GateIn words
(OCR, and Python)
AQA symbolsLogic symbols
(maths, A level, some textbooks)
Python
NOTNOT AA (bar over it)¬Anot a
ANDA AND BA . BA ∧ Ba and b
ORA OR BA + BA ∨ Ba or b
XORA XOR BA ⊕ BA ⊕ Ba != b
★Exams

AQA 8525 uses . for AND, + for OR, ⊕ for XOR and an overbar for NOT. Their own example: (A AND B) OR (NOT C) is written A.B + C.

OCR J277 writes the words AND, OR, NOT. It says other valid notation is accepted too, such as T/F for 1/0, or ∨ for OR. The ∧ ∨ ¬ symbols are the ones mathematicians use.

!Watch out

In AQA notation, + means OR, not add. So 1 + 1 = 1 here. And the dot is AND, not "times"… even though, for 0 and 1, AND really does give the same answer as times (Step 5).

Which part do you work out first?

  1. Brackets first, like in maths.

  2. Then NOT. It sticks to the thing right after it.

  3. Then AND, then OR. Exam questions nearly always add brackets so you don't have to rely on this.

e.g.Example

NOT A AND B means (NOT A) AND B. It does not mean NOT (A AND B). Try A = 0, B = 0:

(NOT 0) AND 0 = 1 AND 0 = 0.

NOT (0 AND 0) = NOT 0 = 1. Different answers, so brackets matter.

Combining gates into circuits

The output of one gate can feed into another. Work out the circuit one gate at a time, from the inputs on the left to Q on the right.

▶

Circuit simulator

wire carrying 1wire carrying 0A dot joins wires. Wires that cross without a dot are not joined.

The truth table fills in as you try each input pattern. Tap a row to jump to it.

e.g.Example

A car alarm sounds if the alarm is set, AND either a door is open OR a window is broken: Q = S AND (D OR W). Pick "Car alarm" above and find the 3 rows where it sounds.

★Remember

For a big table, add a column for each middle wire (P, R…). Fill those first, then use them to fill Q. It stops mistakes.

From circuit to expression, and back

Exams ask both ways. Write the circuit as an expression, or draw the circuit for an expression.

Label every wire as you go. The label on the last wire is the answer.

Circuit → expression

  1. Start at the gates that only touch inputs. Here: the OR gate (A, B) and the NOT gate (C).

  2. Write what leaves each one: A OR B and NOT C.

  3. Move right. The AND gate takes those two wires. Put each one in brackets if it has more than one part.

  4. Answer: Q = (A OR B) AND NOT C. In AQA symbols: Q = (A + B) . C.

Expression → circuit

  1. Find the operator that is done last: the one outside all brackets. In (A OR B) AND NOT C it is the AND. Draw it on the right, next to Q.

  2. Its two inputs are the two sides: (A OR B) and NOT C.

  3. Draw a gate for each side: an OR gate fed by A and B, and a NOT gate fed by C.

  4. Join their outputs to the AND gate. Check one row: A = 1, B = 0, C = 0 → (1 OR 0) AND NOT 0 = 1 AND 1 = 1.

!Watch out

NOT (A AND B) is one AND gate, then a NOT. (NOT A) AND (NOT B) is two NOT gates, then an AND. They give different tables: try A = 1, B = 0.

Gates that add: the half adder

Adding two bits gives a 2-bit answer: a carry and a sum. 1 + 1 = 2, which is 10 in binary: carry 1, sum 0.

ABA + BCarrySum
000 = 0000
011 = 0101
101 = 0101
112 = 1010
  1. Look at the Carry column: 0, 0, 0, 1. That is the AND table.

  2. Look at the Sum column: 0, 1, 1, 0. That is the XOR table.

  3. So 1 XOR gate + 1 AND gate = a half adder.

▶

Add two bits with gates

Extension: the full adder

A half adder can't take a carry in from the column to its right. A full adder can. It joins two half adders and an OR gate. It has 3 inputs (A, B, carry in), so its truth table has 2³ = 8 rows.

Half adder 1XOR → sumAND → carry Half adder 2XOR → sumAND → carry A B Cin S1 C1 C2 Cout Sum
Chain 8 full adders, each passing its carry out to the next one's carry in, and you can add two whole bytes. That is what the ALU inside the CPU does.
ABCinA + B + CinCoutSum
000000
001101
010101
011210
100101
101210
110210
111311
?Why?

This is binary addition from Year 8, done by hardware. "1 + 1 + 1 = 11: write 1, carry 1" is the last row of the full adder table.

Real chips, and the transistors inside

A gate is not a program. It is a real circuit made of transistors: electronic switches with no moving parts.

+5 V 0 V AB transistor 1transistor 2lamp = Q
A small voltage on the left lead (the base) switches a transistor on. Current can then flow from top to bottom. The two transistors are in a line (series), so current reaches the lamp only if A AND B are both 1. It is the same idea as the series switches in Step 1. But here the switches are pressed by electrical signals, so one gate's output can switch the next gate.
  1. The first transistor was made in 1947 at Bell Labs. Today a phone's processor has billions on a chip the size of a fingernail.

  2. Real chips use CMOS: pairs of transistors. A CMOS NAND gate needs 4 transistors. An AND gate is a NAND plus a NOT, so it needs 6.

  3. You can buy gates on small chips. The 74HC08 has four AND gates. The 74HC32 has four OR gates, and the 74HC86 has four XOR gates. All three use the same pin layout.

Seen from above, the notch on the left. Pins are numbered anticlockwise from pin 1. Each colour is one gate: two inputs (A, B) and one output (Y). Pin 14 = power, pin 7 = ground.

Logic gates in Python and on the micro:bit

The words and, or and not in your programs are the same gates. Python works them out with True and False instead of 1 and 0.

from microbit import *

while True:
    a = button_a.is_pressed()      # True or False
    b = button_b.is_pressed()
    if a and b:                     # AND: both buttons
        display.show(Image.HAPPY)
    elif a != b:                    # XOR: exactly one button
        display.show("1")
    else:                           # neither button
        display.clear()
  1. button_a.is_pressed() returns a Boolean: True or False.

  2. a and b is an AND gate. Both buttons → happy face.

  3. a != b means "a is not equal to b": true when they are different. That's XOR. Python has no xor keyword.

  4. Order matters. The AND test is first, so pressing both shows the face, not "1".

Print a truth table with Python

for a in (0, 1):
    for b in (0, 1):
        print(a, b, a and b, a or b, a ^ b)
0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0
!Watch out

not 1 gives False, not 0. For a 0/1 answer use 1 - a. And ^ is XOR in Python, not "to the power of": that's **.

e.g.Example

if age >= 13 and has_permission: is an AND gate with two inputs. Each comparison (like age >= 13) first turns a number into a Boolean.

Common mix-ups

The mistakes almost everyone makes at first. Read the left, then the right.

People think

OR means one or the other, but not both.

Actually

That's XOR. OR gives 1 when both are 1 too.

People think

In A + B, the + means add, so 1 + 1 = 2.

Actually

In Boolean notation + means OR. 1 + 1 = 1.

People think

3 inputs need 6 rows (2 × 3).

Actually

2 to the power of 3 = 8 rows. Each input doubles the rows.

People think

NAND means NOT A AND NOT B.

Actually

NAND = NOT (A AND B): do the AND, then flip it. A = 1, B = 0 gives NAND = 1, but (NOT A) AND (NOT B) = 0.

People think

NOT needs two inputs, like the others.

Actually

NOT has one input. Its truth table has only 2 rows.

People think

Logic gates are little bits of software.

Actually

They are physical circuits made of transistors. Software is only instructions that the gates carry out.

People think

A half adder can add two numbers.

Actually

It adds two bits only, with no carry in. You need a chain of full adders for bigger numbers.

Check yourself

Quick questions first. Then endless practice: a wrong answer shows you the working, step by step.

[ {"q":"A AND B, with A = 1 and B = 0, gives…","o":["0","1"],"a":0,"w":"AND needs both inputs to be 1."}, {"q":"A OR B, with A = 0 and B = 1, gives…","o":["0","1"],"a":1,"w":"OR needs at least one 1."}, {"q":"How many rows in a truth table with 3 inputs?","o":["3","6","8"],"a":2,"w":"2³ = 2 × 2 × 2 = 8."}, {"q":"Which gate gives 1 only when its two inputs are **different**?","o":["OR","XOR","NAND"],"a":1,"w":"XOR = exclusive OR. 0,1 and 1,0 give 1; 0,0 and 1,1 give 0."}, {"q":"In AQA notation, what does **A + B** mean?","o":["A add B","A OR B","A AND B"],"a":1,"w":"AQA uses + for OR, . for AND, ⊕ for XOR and an overbar for NOT."}, {"q":"A half adder is made from…","o":["an XOR gate and an AND gate","two OR gates","a NOT gate and an OR gate"],"a":0,"w":"XOR gives the sum bit; AND gives the carry bit."}, {"q":"Q = (A OR B) AND NOT C. With A = 0, B = 1, C = 1, Q is…","o":["0","1"],"a":0,"w":"A OR B = 1. NOT C = 0. 1 AND 0 = 0."}, {"q":"What is inside a logic gate on a real chip?","o":["Transistors","A tiny program","Magnets"],"a":0,"w":"Gates are built from transistors: electronic switches. A CMOS NAND gate uses 4."} ]