O Level Computer Science 2210 · Area 2 · Algorithms, programming and logic

Logic gates and truth tables

Logic gates turn binary inputs into a single binary output, and every question on this topic is a translation between three forms: a statement in words, a circuit diagram, and a truth table. You need the six symbols and their exact behaviour, and a reliable method for moving between the forms without simplifying. Marks are lost by mixing up NAND with NOR, by writing only four rows for a three-input table, and by leaving brackets out of an expression.

Not started

Syllabus coverage

  • Identify and use the standard symbols for logic gates
  • Define and understand the functions of NOT, AND, OR, NAND, NOR and XOR (EOR)
  • Use logic gates to create a given logic circuit from a problem statement, a logic expression or a truth table
  • Complete a truth table from a problem statement, a logic expression or a logic circuit
  • Write a logic expression from a problem statement, a logic circuit or a truth table
  • Work with circuits of up to three inputs and one output, drawn as given without simplification

Part of the O Level Computer Science syllabus 2210.

Logic gates and truth tablesThe truth table lists every input combination and the output each one produces.Circuits are drawn as given, with no simplification, and are limited to three inputs.NOTgateANDgateORgateNANDgateNORgateXORgateNOT has one input; every other gate here has two. The circuit is drawn as given, with no simplification.Truth table for A AND B AND CABCOutput000001010011100101110111From a circuit to a truth table1 label every gate output2 write every input combination3 work through gate by gateThe output column is the partyou complete in the exam.
The six logic gate symbols - NOT, AND, OR, NAND, NOR and XOR - drawn in a row above a three-input truth table whose output column is left blank.

Binary inputs, binary outputs

A logic gate is a circuit that takes one or two binary inputs and produces one binary output. The inputs and the output are 0 or 1, which stand for FALSE and TRUE, off and on, low and high. Because the values are binary, the behaviour of a gate can be written out completely: there are only a few possible combinations of inputs, and each one has a fixed output.

Gates are the hardware behind the logical operators in a program. When a program tests `IF Armed = TRUE AND DoorOpen = TRUE`, the processor is using the same AND function that the gate performs, and when a circuit decides whether to sound an alarm, it combines sensor signals with gates in exactly the way the program combines conditions.

Two conventions run through the topic. A circuit in this syllabus has at most three inputs and exactly one output, and circuits are drawn as the problem describes them, without simplification. If a statement asks for `(A AND B) OR C`, the answer has three gates in that arrangement, even if a shorter circuit could produce the same output.

ValueMeansAlso written as
1TRUE, on, high voltageSet, asserted
0FALSE, off, low voltageClear, not asserted

NOT: the inverter

NOT is the only gate in the syllabus with a single input. Its output is the opposite of its input: a 1 becomes a 0 and a 0 becomes a 1. The symbol is a triangle with a small circle on the output, and the circle is the part that means 'inverted'.

In an expression it is written `NOT A`, and in a truth table it has only two rows because there is only one input. If a sensor gives 0 while a door is closed, `NOT` on that sensor gives 1 while the door is closed, which is how a circuit can be built for 'the alarm sounds when the door is not closed'.

Do not confuse NOT with a gate that has two inputs and happens to invert one of them. NOT has one input and one output, full stop.

ANOT A
01
10

AND: both inputs must be 1

AND has two inputs and gives 1 only when both inputs are 1. In every other case the output is 0. The symbol is a D shape with a straight back and a curved front.

AND is the gate for conditions that must all be met. A car's warning light that comes on when the door is open AND the engine is running needs an AND gate, because either condition alone must not light it. In a program the same rule is written with the keyword AND, and the truth table is the definition of what that keyword does.

It is worth memorising the shape of the table rather than the sentence: one row of the four has a 1, and it is the bottom row, where both inputs are 1.

ABA AND B
000
010
100
111

OR: at least one input is 1

OR has two inputs and gives 1 when at least one of them is 1. It gives 0 only when both inputs are 0. The symbol is a curved shape with a pointed output, like an arrowhead with a curved back.

OR is the gate for alternatives. An alarm that sounds when the door sensor OR the window sensor is triggered needs an OR gate, because either sensor on its own is enough. Note that OR also gives 1 when both inputs are 1 - it is not 'one or the other but not both'. That exclusive version is a different gate, XOR.

Again, learn the table by its shape: three of the four rows have a 1, and the only 0 is the top row.

ABA OR B
000
011
101
111

NAND and NOR: AND and OR with the output inverted

NAND is AND followed by NOT. Its output is the opposite of AND, so it gives 0 only when both inputs are 1, and 1 in every other case. The symbol is the AND shape with a small circle on the output. The name says it: Not AND.

NOR is OR followed by NOT. Its output is the opposite of OR, so it gives 1 only when both inputs are 0, and 0 in every other case. The symbol is the OR shape with a circle on the output.

The circle is the whole difference between the pairs, and it is where most marks are lost. Read the symbol carefully: a D shape with a circle is NAND, a curved arrowhead with a circle is NOR. If you can state AND and OR, you can derive NAND and NOR by inverting every output - which is also a safe way to check an answer under pressure.

ABA NAND BA NOR B
0011
0110
1010
1100
NAND and NOR are AND and OR with the output flipped. Work out the plain gate first, then invert the last column - it is faster and safer than remembering four more rows.

XOR: the inputs must be different

XOR (also written EOR, exclusive OR) gives 1 when the two inputs are different, and 0 when they are the same. So 0 and 1 gives 1, 1 and 0 gives 1, but 0 and 0 gives 0 and 1 and 1 gives 0. The symbol is the OR shape with an extra curved line behind the input side.

XOR is the gate for 'one or the other but not both'. A stair light controlled from two switches uses XOR: the light is on when exactly one switch is up. It is also the gate behind binary addition without the carry, because the sum bit of 1 + 1 is 0 with a carry, which is exactly what XOR gives.

The contrast with OR is the examinable point. OR gives 1 for three rows out of four; XOR gives 1 for two. Both give 1 when the inputs differ, but only OR gives 1 when both are 1.

ABA XOR B
000
011
101
110

The six gates side by side

The whole topic rests on this one table. Learn the description of each gate, the shape of its symbol and the pattern of its output column, and every question becomes a matter of applying them in order.

For a circuit with more than one gate, work out the output of each gate in turn and label the wire between gates with its value. A three-input circuit has two gates feeding a third, so there are two intermediate values before the final output. Writing them down is not extra work - it is what stops the last gate being fed the wrong input.

GateOutput is 1 whenRows with output 1 (of four)
NOTthe single input is 0one of two
ANDboth inputs are 1one of four
ORat least one input is 1three of four
NANDthe inputs are not both 1three of four
NORboth inputs are 0one of four
XORthe inputs are differenttwo of four

Building a truth table

A truth table lists every possible combination of inputs and the output for each. For n inputs there are 2^n rows: two rows for one input, four for two inputs, eight for three. A three-input table has eight rows, and a table with only four is wrong however good the logic is.

The rows follow a fixed order so that no combination is missed. The rightmost input column alternates every row: 0, 1, 0, 1 and so on. The next column to the left changes every two rows, and the leftmost changes every four. For three inputs the first column is 0000 1111, the second 0011 0011 and the third 0101 0101, which gives 000, 001, 010, 011, 100, 101, 110, 111.

Add a column for each intermediate point in the circuit before the output column. For `X = (A AND B) OR NOT C`, work out `A AND B` in one column and `NOT C` in another, then OR the two columns into X. Each final value is then a single, small step from two values you can see, instead of a guess.

Completing a table from a circuit uses the same columns, filled by following the wires from left to right. Completing one from a problem statement needs one extra step: decide which inputs the statement names, then write the condition for each row in words before turning it into 0 or 1.

Eight rows for three inputs, four for two. Count the input columns first and write 2^n rows before you work out a single output.

From words to a circuit, and back

A problem statement gives the conditions in English, and you turn them into gates. Identify the inputs first - each sensor or switch is one input - then find the connecting words. AND joins conditions that must both hold, OR joins alternatives, and NOT applies to a single condition that must not hold. Brackets in the resulting expression tell you which gate feeds which.

'The alarm sounds when the system is armed and either the door or the window is open' has three inputs: Armed, Door, Window. 'Either ... or' is an OR between Door and Window, and 'and' joins that result to Armed, so the expression is `Alarm = Armed AND (Door OR Window)`. The brackets are not decoration: without them the expression would mean something else.

Going the other way, from a circuit to an expression, label every wire. Give each gate output a name or write the expression for it beside the gate, starting with the gates nearest the inputs. The last gate gives the whole expression, and any group that feeds a single input must be bracketed.

From a truth table to an expression, find the rows where the output is 1. Each such row becomes a term joining the inputs with AND, using NOT for any input that is 0 in that row; the terms are then joined with OR. A table with one row of 1 gives one term; a table with three gives three. No simplification is expected, so write the terms as they fall out of the rows.

  • Name the inputs before anything else - one input per sensor, switch or condition.
  • AND for conditions that must all hold; OR for alternatives; NOT for a condition that must not hold.
  • Bracket any group of gates that feeds a single input of another gate.
  • From a truth table, write one AND term for each row with output 1, then OR the terms together.

Checking your answer

Every logic answer can be checked, because the number of input combinations is small. Pick a row - usually the one where all inputs are 1, then the one where all are 0 - and follow it through the circuit or expression by hand. If the result does not match your table, the table is wrong.

Check the gate count too. A circuit built from a three-input expression written without simplification has one gate for each operator plus one for each NOT. `(A AND B) OR NOT C` needs three gates: AND, NOT and OR. If your drawing has two gates, an operator has been lost; if it has five, something has been invented.

Finally, check the direction words. NAND and NOR are the pairs most often swapped, and XOR is most often confused with OR. A quick test on the row where both inputs are 1 separates them all: AND and OR give 1, NAND and XOR give 0, NOR gives 0 and NOT does not apply.

Test rowANDORNANDNORXOR
A = 0, B = 000110
A = 1, B = 111000

Key terms you must be able to define

Logic gate
A circuit with one or two binary inputs and one binary output, whose output is fixed by the combination of inputs.
Boolean
A value that is either TRUE or FALSE, written as 1 or 0 in a logic circuit.
Truth table
A table listing every possible combination of inputs and the output for each; it has 2^n rows for n inputs.
NOT
The single-input gate that inverts its input: 0 becomes 1 and 1 becomes 0.
AND
The gate that outputs 1 only when both inputs are 1.
OR
The gate that outputs 1 when at least one input is 1, and 0 only when both are 0.
NAND
AND with the output inverted: it outputs 0 only when both inputs are 1.
NOR
OR with the output inverted: it outputs 1 only when both inputs are 0.
XOR (EOR)
Exclusive OR: it outputs 1 when the two inputs are different and 0 when they are the same.
Logic expression
A statement of a circuit using the gate names and brackets, such as X = (A AND B) OR NOT C.

In context

2210 sets its questions inside systems a student already uses - a school network, a phone, a supermarket till, a vending machine, a website. These show how the theory is applied to a context you have not seen before, then you answer for the system you are given.

A car's seat-belt warning

The warning light comes on when the ignition is on AND the driver's seat is occupied AND the belt is not fastened. With inputs Ignition, Occupied and BeltFastened, the expression is `Light = Ignition AND Occupied AND NOT BeltFastened`. The NOT applies to the belt sensor alone, so it is bracketed with that input, not with the whole expression.

A burglar alarm

The alarm sounds when the system is armed and either the door or a window is opened: `Alarm = Armed AND (Door OR Window)`. Two sensors feed an OR gate, and its output feeds an AND gate with Armed. It is a three-input circuit with three gates, drawn exactly as the statement reads and not simplified.

A stair light

A light controlled by a switch at the bottom and a switch at the top is on when exactly one switch is up, so the two switch signals feed an XOR gate. Both up or both down gives 0. If the same two signals fed an OR gate, the light would stay on when both switches were up, which is not how a stair light behaves.

A machine safety interlock

A machine runs only when the guard is closed and the start button is pressed and the stop button is not pressed: `Run = Guard AND Start AND NOT Stop`. A test row with Guard = 1, Start = 1 and Stop = 1 gives 1 AND 1 AND 0 = 0, so the machine stops even with the button held - which is the behaviour a truth table is used to prove.

Flashcards

Click a card to flip it. Mark honestly — the tracker uses this.

Q1Which logic gate has only one input?
NOT. It inverts its single input, so 0 becomes 1 and 1 becomes 0.
Q2When does an AND gate output 1?
Only when both inputs are 1. Every other combination gives 0.
Q3When does an OR gate output 0?
Only when both inputs are 0. If either input is 1, the output is 1.
Q4What is the difference between AND and NAND?
NAND is AND with the output inverted, so it gives 0 only when both inputs are 1 and 1 on the other three rows.
Q5When does a NOR gate output 1?
Only when both inputs are 0. On every other row the output is 0.
Q6When does an XOR gate output 1?
When the two inputs are different: 0 and 1, or 1 and 0. It gives 0 when both inputs are the same.
Q7How many rows does a truth table need for three inputs?
Eight, because 2^3 = 8. In general, n inputs need 2^n rows.
Q8In what order are the input combinations written in a truth table?
Binary counting order: the rightmost column alternates every row, the next every two rows, the leftmost every four. For three inputs: 000, 001, 010, 011, 100, 101, 110, 111.
Q9How do you write an expression from a truth table?
For each row with output 1, write an AND term of all the inputs, using NOT on any input that is 0. Then OR the terms together.
Q10What does the small circle on a gate symbol mean?
It inverts the output. AND with a circle is NAND; OR with a circle is NOR.
Q11Why should a circuit not be simplified in an exam answer?
The syllabus asks for the circuit described by the statement or expression. Simplifying changes the gates, so the drawing no longer matches what was asked for.
0 of 11 marked as known

Exam-style questions

7 questions, 24 marks. Write your answer and press Mark my answer — it is marked against the scheme and tells you which points you made and what would gain the next mark. You can still reveal the scheme and mark it yourself instead.

StateAO11 mark
State the name of the logic gate whose output is 1 only when its two inputs are different.

Mark scheme — tick each point you actually made

Examiner tip: The key words are 'different' for XOR and 'both' for AND. OR would give 1 when the inputs are different too, but also when both are 1.
StateAO12 marks
State the output of a NOR gate when both of its inputs are 0, and state the output when both of its inputs are 1.

Mark scheme — tick each point you actually made

Examiner tip: NOR is OR inverted, so work out OR first and flip it. OR gives 0 then 1, so NOR gives 1 then 0.
GiveAO24 marks
A NAND gate has inputs A and B and output X. Give the complete truth table for the gate.

Mark scheme — tick each point you actually made

Examiner tip: Four rows for two inputs, in binary counting order. Write AND's output first in your head and invert it: 0, 0, 0, 1 becomes 1, 1, 1, 0.
GiveAO23 marks
An alarm sounds when the system is armed and either the door sensor or the window sensor detects an opening. The inputs are Armed, Door and Window, and the output is Alarm. Give the logic expression for the alarm.

Mark scheme — tick each point you actually made

Examiner tip: 'Either ... or' is OR and 'and' joins the armed condition to it. Without brackets the expression means something different, so the brackets are worth a mark.
DescribeAO24 marks
A circuit has inputs A, B and C and output X, where X = (A AND B) OR NOT C. Describe the gates and the connections needed to build this circuit.

Mark scheme — tick each point you actually made

Examiner tip: Describe it gate by gate from the inputs towards the output, and name what feeds each input of the final OR gate. The bracket tells you the AND gate's output is one whole input.
DescribeAO25 marks
A student must complete the truth table for a circuit with three inputs, A, B and C. Describe the steps the student should take.

Mark scheme — tick each point you actually made

Examiner tip: The marks are for the method: eight rows, ordered inputs, intermediate columns, left to right, final column. Saying 'work it out' earns nothing.
GiveAO25 marks
A truth table for inputs A, B and C has an output X that is 1 only for the rows A = 0, B = 1, C = 1 and A = 1, B = 1, C = 0. Give the logic expression for X.

Mark scheme — tick each point you actually made

Examiner tip: One AND term per row with output 1, inverting the inputs that are 0 in that row, then OR the terms. No simplification is needed - write the terms as the rows give them.

Exam technique for this topic

  • Count the inputs and write 2^n rows before you start a truth table: two inputs means four rows, three inputs means eight. A three-input table with four rows loses most of its marks at once.
  • Fill the input columns in the fixed order - rightmost alternates every row, the next every two rows, the leftmost every four - so no combination is missed or repeated.
  • Put a circle on the output of the gate you mean: NAND is AND with a circle, NOR is OR with a circle. Draw it deliberately, because a missing circle turns one gate into another.
  • Bracket every group that feeds one input of another gate. `A AND B OR C` and `A AND (B OR C)` are different circuits, and only the second matches 'A and either B or C'.
  • Do not simplify. The syllabus asks for the circuit the statement describes, so a three-gate answer is correct even when a two-gate circuit would give the same output.

Why students lose marks here

A student wroteA student drew a truth table for a three-input circuit with four rows.
Why it loses marksFour rows cover only two inputs. With three inputs there are 2^3 = 8 combinations, so four of them are missing and the output column cannot be complete.
The answer that scoresA three-input table has eight rows, running 000, 001, 010, 011, 100, 101, 110, 111. The number of rows is 2 raised to the number of inputs, so count the input columns first and write the rows before working anything out.
A student wroteA student said a NOR gate gives 1 when both inputs are 1.
Why it loses marksThat is the opposite of NOR. NOR is OR with the output inverted, and OR already gives 1 when both inputs are 1, so NOR gives 0 on that row.
The answer that scoresNOR gives 1 only when both inputs are 0; on every other row it gives 0. NAND is the gate that gives 0 only when both inputs are 1. Checking the row where both inputs are 1 separates every gate: AND and OR give 1, NAND, NOR and XOR give 0.
A student wroteA student wrote the expression for 'the alarm sounds when armed and either the door or the window is open' as `Alarm = Armed AND Door OR Window`.
Why it loses marksWithout brackets the expression groups as `(Armed AND Door) OR Window`, so an open window sounds the alarm even when the system is not armed. The brackets decide which gate feeds which.
The answer that scoresThe OR applies to the two sensors and the AND applies to Armed and that result: `Alarm = Armed AND (Door OR Window)`. Any group of gates that feeds a single input of another gate must be bracketed.
A student wroteA student simplified `(A AND B) OR (A AND C)` to `A AND (B OR C)` in an answer.
Why it loses marksThe simplification is mathematically correct, but the question asked for the circuit described by the given expression. Simplifying changes the number and arrangement of gates, so the drawing no longer matches what was asked for.
The answer that scoresDraw the circuit exactly as the expression is written: two AND gates feeding an OR gate. Circuits in this syllabus are not simplified, and the marks are for the gates the statement names.
A student wroteA student wrote `X = A XOR B` for a statement that said 'X is 1 when A is 1 or B is 1'.
Why it loses marksXOR gives 0 when both inputs are 1, so it does not match 'A is 1 or B is 1'. The statement describes inclusive OR, which gives 1 on that row as well.
The answer that scoresThe correct expression is `X = A OR B`. XOR would be right only if the statement said 'one or the other but not both', or 'the inputs are different'.

Where this fits in O Level Computer Science 2210

This page is one topic of Cambridge O Level Computer Science 2210. O Level Computer Science 2210 is the course home: every area and every topic in Cambridge's own order, the papers you sit and what each one tests, and the exam-practice tools. The full topic list, with each topic linked to its revision page, is on the O Level Computer Science 2210 syllabus page.