The six logic gates
IGCSE Computer Science · Boolean Logic
What a logic gate does
A logic gate takes one or two binary inputs ($0$ or $1$) and produces one binary output, following a fixed rule. Every digital circuit is built from them.
NOT
One input. The output is the opposite of the input.
| $A$ | $0$ | $1$ |
| output | $1$ | $0$ |
AND
Output is $1$ only when both inputs are $1$.
| $A$ | $0$ | $0$ | $1$ | $1$ |
| $B$ | $0$ | $1$ | $0$ | $1$ |
| out | $0$ | $0$ | $0$ | $1$ |
OR
Output is $1$ when at least one input is $1$ — including when both are.
| $A$ | $0$ | $0$ | $1$ | $1$ |
| $B$ | $0$ | $1$ | $0$ | $1$ |
| out | $0$ | $1$ | $1$ | $1$ |
XOR — exclusive OR
Output is $1$ when the inputs are different.
| $A$ | $0$ | $0$ | $1$ | $1$ |
| $B$ | $0$ | $1$ | $0$ | $1$ |
| out | $0$ | $1$ | $1$ | $0$ |
Compare the last row of OR and XOR. They agree on the first three rows and differ on exactly one: when both inputs are $1$, OR gives $1$ but XOR gives $0$.
In everyday English, OR is "tea or coffee — and you may have both"; XOR is "tea or coffee, but not both".
NAND and NOR
NAND is AND with the output inverted. NOR is OR with the output inverted.
| $A$ | $0$ | $0$ | $1$ | $1$ |
| $B$ | $0$ | $1$ | $0$ | $1$ |
| NAND | $1$ | $1$ | $1$ | $0$ |
| NOR | $1$ | $0$ | $0$ | $0$ |
The reliable method: work out AND or OR first, then flip every value. Do not try to memorise NAND and NOR separately — deriving them is faster and never goes wrong.
Two useful facts that follow: NAND gives $0$ only when both inputs are $1$; NOR gives $1$ only when both inputs are $0$.
Size of a truth table
A truth table must list every possible combination of inputs. With $n$ inputs there are
$$2^n \text{ rows}$$
So $2$ inputs give $4$ rows, $3$ inputs give $8$ rows, and $4$ inputs give $16$.
The common error is writing $2n$ — giving $6$ rows for $3$ inputs. A table with missing rows is incomplete and loses marks even where the rows present are right.
Fill the input columns in binary counting order — $000$, $001$, $010$, $011$, $100$, … — so no combination is missed.
Worked example: building a truth table for an expression
Build the full truth table for the expression $X = (A \ \text{AND} \ B) \ \text{OR} \ (\text{NOT} \ C)$, with three inputs $A$, $B$, $C$.
Solution
Step 1 — determine the number of rows.
There are $3$ inputs, so the number of rows is $2^3 = 8$.
Note it is not $2 \times 3 = 6$. A table with missing rows is incomplete and loses marks even if the rows written are all correct.
Step 2 — fill in the inputs in binary counting order.
$000$, $001$, $010$, $011$, $100$, $101$, $110$, $111$. Working in this order, you are certain not to miss any combination.
Step 3 — add the intermediate columns.
This is the most important step technically: do not calculate straight to the final result, but split the expression into parts.
Two intermediate columns are needed: $A \ \text{AND} \ B$, and $\text{NOT} \ C$.
Step 4 — fill in each column.
The column $A \ \text{AND} \ B$: equals $1$ only when both equal $1$. For the eight rows above, it is $0,0,0,0,0,0,1,1$ — only the last two rows, since they are the only rows with $A = 1$ and $B = 1$.
The column $\text{NOT} \ C$: invert the column $C$. Column $C$ is $0,1,0,1,0,1,0,1$, so $\text{NOT} \ C$ is $1,0,1,0,1,0,1,0$.
The result column $X$: combine the two columns above with an OR gate — equals $1$ when at least one of the two equals $1$.
Combining row by row: $1, 0, 1, 0, 1, 0, 1, 1$.
The completed table.
$ABC = 000$: $A\text{AND}B = 0$, $\text{NOT}C = 1$ → $X = 1$
$ABC = 001$: $0$, $0$ → $X = 0$
$ABC = 010$: $0$, $1$ → $X = 1$
$ABC = 011$: $0$, $0$ → $X = 0$
$ABC = 100$: $0$, $1$ → $X = 1$
$ABC = 101$: $0$, $0$ → $X = 0$
$ABC = 110$: $1$, $1$ → $X = 1$
$ABC = 111$: $1$, $0$ → $X = 1$
Check a different way. The row $ABC = 111$ is the most worthwhile to check, because the two intermediate columns differ there. $A \ \text{AND} \ B = 1$ but $\text{NOT} \ C = 0$; an OR gate needs only one value of $1$, so $X = 1$. Correct.
Why it is better to use intermediate columns: if you work out $X$ directly in your head, you have to hold two calculations in your mind at once across eight rows — and one wrong row ruins it.
Worked example: deriving NAND and NOR
Rather than memorising, derive the truth tables of the NAND gate and the NOR gate, then state one memorable characteristic of each gate.
Solution
Method: calculate the base gate first, then invert every value.
The NAND gate.
Step 1 — write the AND gate. It equals $1$ only when both inputs equal $1$:
$0,0$ → $0$ | $0,1$ → $0$ | $1,0$ → $0$ | $1,1$ → $1$
Step 2 — invert each value.
$0,0$ → $1$ | $0,1$ → $1$ | $1,0$ → $1$ | $1,1$ → $0$
A memorable characteristic: the NAND gate gives $0$ only when both inputs are $1$. The other three cases all give $1$.
The NOR gate.
Step 1 — write the OR gate. It equals $1$ when at least one input equals $1$:
$0,0$ → $0$ | $0,1$ → $1$ | $1,0$ → $1$ | $1,1$ → $1$
Step 2 — invert each value.
$0,0$ → $1$ | $0,1$ → $0$ | $1,0$ → $0$ | $1,1$ → $0$
A memorable characteristic: the NOR gate gives $1$ only when both inputs are $0$. The other three cases all give $0$.
Why derive instead of memorise.
Memorising the four two-input truth tables separately means remembering sixteen separate numbers, and it is very easy to confuse one gate with another under exam pressure.
Deriving needs you to remember only two base rules — the AND gate needs both, the OR gate needs at least one — plus one inversion operation.
This method never goes wrong, and it is also quicker in the exam.
A quick check by symmetry: the two memorable characteristics above are beautifully symmetrical — the NAND gate gives $0$ only in the row $1,1$; the NOR gate gives $1$ only in the row $0,0$. If you derive them and do not see that symmetry, you have certainly gone wrong somewhere.