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.
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.
In 1937, Claude Shannon noticed that Boole's maths fits electrical switches perfectly. Switch on = true = 1. Switch off = false = 0.
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
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.
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.
| A | NOT A |
|---|---|
| 0 | 1 |
| 1 | 0 |
| A | B | A AND B | A OR B |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 |
NOT has one input. It flips it: 0 → 1, 1 → 0. It is also called an inverter.
AND gives 1 only if both inputs are 1. One 0 is enough to make it 0.
OR gives 1 if at least one input is 1. It is only 0 when both are 0.
"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.
| A | B | A XOR B | A NAND B | A NOR B |
|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 1 |
| 0 | 1 | 1 | 1 | 0 |
| 1 | 0 | 1 | 1 | 0 |
| 1 | 1 | 0 | 0 | 0 |
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.
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 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.
Count the inputs: n.
Each input is 0 or 1: 2 choices. So there are 2 × 2 × … = 2ⁿ rows.
Write the row numbers 0 up to 2ⁿ − 1 in binary, with n digits. Those are your input columns.
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
n = 3 inputs: A, B and C.
R = 2³ = 2 × 2 × 2 = 8 rows.
Count from 0 to 8 − 1 = 7 in 3-bit binary: 000, 001, 010, 011, 100, 101, 110, 111.
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.
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.
| A | B | AND A × B | OR A + B − A × B | XOR (A + B) mod 2 |
|---|---|---|---|---|
| 0 | 0 | 0 × 0 = 0 | 0 + 0 − 0 = 0 | 0 mod 2 = 0 |
| 0 | 1 | 0 × 1 = 0 | 0 + 1 − 0 = 1 | 1 mod 2 = 1 |
| 1 | 0 | 1 × 0 = 0 | 1 + 0 − 0 = 1 | 1 mod 2 = 1 |
| 1 | 1 | 1 × 1 = 1 | 1 + 1 − 1 = 1 | 2 mod 2 = 0 |
By hand: A = 1, B = 1
AND: Q = A × B = 1 × 1 = 1. Multiplying by 0 always gives 0, so one 0 is enough to make AND give 0.
OR: start with A + B = 1 + 1 = 2. That's too big: an output can only be 0 or 1.
Take away the "both are 1" case once: 2 − (1 × 1) = 2 − 1 = 1.
XOR: A + B = 2. "mod 2" means the remainder after dividing by 2. 2 ÷ 2 = 1 remainder 0. So Q = 0.
NOT A: Q = 1 − A = 1 − 1 = 0.
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.
| Gate | In words (OCR, and Python) | AQA symbols | Logic symbols (maths, A level, some textbooks) | Python |
|---|---|---|---|---|
| NOT | NOT A | A (bar over it) | ¬A | not a |
| AND | A AND B | A . B | A ∧ B | a and b |
| OR | A OR B | A + B | A ∨ B | a or b |
| XOR | A XOR B | A ⊕ B | A ⊕ B | a != b |
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.
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?
Brackets first, like in maths.
Then NOT. It sticks to the thing right after it.
Then AND, then OR. Exam questions nearly always add brackets so you don't have to rely on this.
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
The truth table fills in as you try each input pattern. Tap a row to jump to it.
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.
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.
Circuit → expression
Start at the gates that only touch inputs. Here: the OR gate (A, B) and the NOT gate (C).
Write what leaves each one: A OR B and NOT C.
Move right. The AND gate takes those two wires. Put each one in brackets if it has more than one part.
Answer: Q = (A OR B) AND NOT C. In AQA symbols: Q = (A + B) . C.
Expression → circuit
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.
Its two inputs are the two sides: (A OR B) and NOT C.
Draw a gate for each side: an OR gate fed by A and B, and a NOT gate fed by C.
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.
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.
| A | B | A + B | Carry | Sum |
|---|---|---|---|---|
| 0 | 0 | 0 = 00 | 0 | 0 |
| 0 | 1 | 1 = 01 | 0 | 1 |
| 1 | 0 | 1 = 01 | 0 | 1 |
| 1 | 1 | 2 = 10 | 1 | 0 |
Look at the Carry column: 0, 0, 0, 1. That is the AND table.
Look at the Sum column: 0, 1, 1, 0. That is the XOR table.
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.
| A | B | Cin | A + B + Cin | Cout | Sum |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 | 0 | 1 |
| 0 | 1 | 0 | 1 | 0 | 1 |
| 0 | 1 | 1 | 2 | 1 | 0 |
| 1 | 0 | 0 | 1 | 0 | 1 |
| 1 | 0 | 1 | 2 | 1 | 0 |
| 1 | 1 | 0 | 2 | 1 | 0 |
| 1 | 1 | 1 | 3 | 1 | 1 |
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.
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.
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.
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.
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()
button_a.is_pressed()returns a Boolean: True or False.a and bis an AND gate. Both buttons → happy face.a != bmeans "a is not equal to b": true when they are different. That's XOR. Python has noxorkeyword.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)
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 **.
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.
OR means one or the other, but not both.
That's XOR. OR gives 1 when both are 1 too.
In A + B, the + means add, so 1 + 1 = 2.
In Boolean notation + means OR. 1 + 1 = 1.
3 inputs need 6 rows (2 × 3).
2 to the power of 3 = 8 rows. Each input doubles the rows.
NAND means NOT A AND NOT B.
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.
NOT needs two inputs, like the others.
NOT has one input. Its truth table has only 2 rows.
Logic gates are little bits of software.
They are physical circuits made of transistors. Software is only instructions that the gates carry out.
A half adder can add two numbers.
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.