A Level Computer Science · Chapter 17 · Component 01: Computer systems
Every decision a computer makes comes down to gates that combine true and false, and Boolean algebra lets you design, check and simplify them.
This chapter builds logic from single gates up to the adders and memory inside a processor. You will learn to move between problems, expressions, circuits and truth tables, and to simplify with the laws of Boolean algebra and Karnaugh maps.
In this chapter · jump to a section
Start here
Your route through this chapter
By the end of this chapter you will be able to:
- draw and recognise the AND, OR, NOT, XOR, NAND and NOR gates, and complete truth tables for them
- write a Boolean expression for a problem, a circuit or a truth table, and draw the circuit for an expression
- apply the laws of Boolean algebra, including De Morgan's laws, distribution, association, commutation and double negation
- simplify expressions step by step, naming the law used at each step
- simplify expressions of up to four inputs with Karnaugh maps
- explain how half adders and full adders add binary numbers, and how a ripple-carry adder detects overflow
- describe how an edge-triggered D-type flip-flop stores one bit
Time: three sessions, about 4 hours, plus the programming task.
You need first: binary numbers and binary addition from Number systems and binary arithmetic, and the idea of a selection statement from programming.
The big picture
The big picture
In July 1969, the computer that guided Apollo 11 to the Moon fitted in a box the size of a briefcase. Its designers at MIT made an unusual choice. They built the logic of the whole machine from just one kind of gate, a three-input NOR gate. There were about 5,600 of them, packed two to a chip.
How can a single gate type do everything a computer needs? The answer is Boolean algebra. It is a mathematics with only two values, true and false, written 1 and 0. It has three basic operations: AND, OR and NOT. Every other operation, from adding numbers to comparing passwords, can be built from these.
The Apollo engineers used a remarkable fact. NOR alone can make NOT, OR and AND. So a factory needed to make only one kind of chip, and could test it to an extreme standard. Reliability mattered more than anything when a fault could strand astronauts.
The same ideas run every phone and laptop today. A modern processor contains billions of transistors arranged as gates. Each gate is a tiny switch circuit that takes bits in and gives one bit out. Combine enough of them and you get adders, comparators and memory.
Boolean algebra also lives in your programs. Each time you write an IF statement with AND or OR, you are writing a Boolean expression. A condition such as "the user is logged in and is not banned" has the same structure as a circuit. Simplifying it makes the code clearer, just as simplifying a circuit makes it cheaper.
Here is a small decision drawn as a circuit. A greenhouse waters its plants when the soil is dry (D) and it is not raining (R), or when someone presses the manual button (M).
The table beside the circuit is a truth table. It lists every combination of inputs and the output for each. Three inputs give eight rows, because each input can be 0 or 1 and 2 × 2 × 2 = 8.
This chapter moves between four views of the same logic. A problem in words becomes an expression. An expression becomes a circuit. Any of these can be checked with a truth table. Then the laws of Boolean algebra let you make the circuit smaller without changing what it does.
Finally, you will see how gates do arithmetic and remember. A half adder adds two bits. Full adders chained together add whole binary numbers. A flip-flop holds one bit until the clock tells it to change. These are the building blocks of the processor itself.
Section 1
Logic gates and truth tables
Two values and three operations
Boolean logic uses only two values. We write them 1 and 0, or TRUE and FALSE. In a circuit they are two voltage levels, such as 5 volts and 0 volts. Nothing in between counts.
The three basic operations each have a name, a symbol and a gate. AND, written ∧, gives 1 only when both inputs are 1. OR, written ∨, gives 1 when at least one input is 1. NOT, written ¬, has one input and flips it.
You may meet other notations in older books. AND is sometimes written A.B or AB, OR as A + B, and NOT as a bar over the letter. They mean exactly the same. This course uses ∧, ∨ and ¬, the symbols of logic.
Truth tables
A truth table lists every possible combination of inputs, one per row, with the output for each. With n inputs there are 2ⁿ rows. Two inputs give four rows, three give eight and four give sixteen.
Always list the rows in binary counting order: 00, 01, 10, 11 for two inputs. This makes it easy to check that no row is missing. It also matches the row numbers used later for Karnaugh maps.
Common mistake: forgetting rows of the truth table. A truth table for n inputs needs 2ⁿ rows. Students often write only the rows they think matter, and miss the case that breaks their circuit. Count in binary so that every combination appears exactly once.
The six gates
Three more gates are used so often that they have their own symbols. XOR, written ⊻, is exclusive OR. It gives 1 when the inputs are different, but 0 when both are 1. NAND is NOT AND, and NOR is NOT OR. The small circle on their outputs means "then invert".
Common mistake: treating XOR as OR. XOR gives 0 when both inputs are 1, but OR gives 1. In everyday English, "or" is often exclusive: "tea or coffee" means one, not both. In logic, OR is inclusive and XOR is the exclusive version.
Worked example 1: a three-input truth table
Complete the truth table for Q = (A ⊻ B) ∧ C.
| Working | Reason |
|---|---|
| Three inputs, so 2³ = 8 rows, from 000 to 111 | One row for every combination of A, B and C |
| Work out A ⊻ B first: 0, 0, 1, 1, 1, 1, 0, 0 | XOR is 1 only when A and B differ |
| AND that column with C: rows 011 and 101 give 1 | Both A ⊻ B and C must be 1 |
| Output column, top to bottom: 00010100 | Q is 1 exactly when C is 1 and A, B differ |
Adding a column for each part of the expression is the safest method. It breaks a hard evaluation into easy ones. Examiners often give partly completed tables with these extra columns.
One gate can make the rest
The Apollo computer relied on a key fact. NAND and NOR are universal: each one alone can build every other gate. For NOR, connect both inputs together and the gate becomes NOT, because ¬(A ∨ A) = ¬A.
Then follow a NOR with a NOT made of NOR, and you have OR. Invert both inputs first, using two more NOR gates, and you have AND. NAND works the same way with the roles swapped. Universal gates make chip manufacture simpler and cheaper.
Gates are switches
Inside a chip, each gate is made of transistors. A transistor acts as a switch that is turned on or off by a voltage. Two switches in series let current through only when both are on, which is AND. Two in parallel let it through when either is on, which is OR.
In the CMOS technology used by almost every modern chip, the cheapest gates to build are NAND and NOR. A NAND gate needs four transistors, while an AND gate needs six, because it is a NAND followed by a NOT. This is another reason designers like to work in NAND or NOR.
A gate takes a tiny time to respond, called its propagation delay. Signals passing through many gates in a row add up these delays. That is why the number of gates between the inputs and the output matters for speed, as you will see with adders.
Check yourself. How many rows does a truth table for a circuit with five inputs need, and why?
Show the answer
It needs 2⁵ = 32 rows. Each of the five inputs can be 0 or 1 independently, so the number of combinations doubles with every extra input.
Section 2
From problems to expressions and circuits
Reading a problem as logic
Real problems arrive in words. To turn them into logic, first name each input with a letter and say what 1 means. Then find the words that act as operators: "and", "or", "not", "unless", "but not" and "either… or… but not both".
For example, a car's interior light L comes on when either door is open, unless the headlights are on. Let D mean the driver's door is open, P the passenger's door, and H the headlights. The light is L = (D ∨ P) ∧ ¬H.
The word "unless" usually means "and not". The word "either" with "but not both" means XOR. Words like "only if" need care, so test your expression against a few rows of the situation.
Precedence
Like ordinary algebra, Boolean algebra has an order of operations. NOT is done first, then AND, then OR. So A ∨ B ∧ C means A ∨ (B ∧ C), just as 2 + 3 × 4 means 2 + 12.
Brackets override the order. When in doubt, add them. A bracket that is not needed costs nothing, but a missing one changes the logic completely. The car light needs its brackets: D ∨ P ∧ ¬H would be D ∨ (P ∧ ¬H), which is wrong.
Common mistake: ignoring precedence. AND is done before OR, so A ∨ B ∧ C is A ∨ (B ∧ C), not (A ∨ B) ∧ C. The two differ when A is 1 and C is 0. Put brackets in whenever you mean the OR to happen first.
Worked example 2: from a problem to an expression
A vending machine releases a drink (V) when a coin is inserted (C) and a button is pressed (B), as long as the machine is not empty (E). An engineer's key (K) also releases a drink at any time. Write an expression for V.
| Working | Reason |
|---|---|
| Inputs: C coin, B button, E empty, K key | Name every input and what 1 means |
| Normal sale: C ∧ B ∧ ¬E | All three conditions must hold together |
| Engineer's key works on its own: ∨ K | Either way of releasing a drink is enough |
| V = C ∧ B ∧ ¬E ∨ K | AND binds first, so no extra brackets are needed |
Test it with two rows. A coin and a button with the machine empty give C ∧ B ∧ ¬E = 0, and with no key V = 0, as it should. A key alone gives V = 1, also correct.
From a circuit to an expression
To read a circuit, work from the inputs towards the output. Write the expression at the output of each gate, one gate at a time. Then the output of the last gate is the whole expression.
Worked example 3: reading a circuit
Write the expression for the circuit below, then find Q when A = 1, B = 1 and C = 0.
| Working | Reason |
|---|---|
| NOT gate output: ¬B | The triangle with a circle inverts B |
| Top AND gate: A ∧ ¬B; lower AND gate: B ∧ C | Each AND combines the two wires entering it |
| Q = A ∧ ¬B ∨ B ∧ C | The OR gate joins the two AND outputs |
| With A = 1, B = 1, C = 0: 1 ∧ 0 ∨ 1 ∧ 0 = 0 | Both AND terms are 0, so the OR gives 0 |
Notice that the input B appears twice in the drawing. In a real circuit the same wire branches to both gates. Drawing it twice keeps the picture clear, and the logic is the same.
From an expression to a circuit
Drawing works the other way. Find the operation done last, using precedence. That becomes the gate at the output. Then draw the circuit for each of its inputs in the same way, working back towards the letters.
Common mistake: misplacing the NOT. ¬A ∧ B inverts only A, but ¬(A ∧ B) inverts the result of the AND. In a circuit, the first has a NOT gate on input A, and the second is a NAND gate. Check exactly what the bar or ¬ covers.
Expressions in programs
Your programs use the same logic. In Python the operators are written and, or and not, and they follow the same precedence. The car light could be coded like this.
light = (driver_open or passenger_open) and not headlights_on
Without the brackets, Python would do the and first, and the light would ignore the headlights whenever the driver's door was open. The bug is identical to the circuit mistake.
From a truth table to an expression
Sometimes you are given only the truth table. You can always write an expression from it. Find every row where the output is 1. Write one AND term for each row, using the input itself if it is 1 and its inverse if it is 0. Then join the terms with OR.
For example, suppose Q is 1 on rows 011 and 101 only. Row 011 gives ¬A ∧ B ∧ C, and row 101 gives A ∧ ¬B ∧ C. So Q = ¬A ∧ B ∧ C ∨ A ∧ ¬B ∧ C. This form is called a sum of products, because it is an OR of AND terms.
A sum of products is always correct, but it is rarely the shortest form. This one is the same function as (A ⊻ B) ∧ C from worked example 1. Section 4 and section 5 show how to shorten such expressions.
Check yourself. A school alarm sounds when a window is open and the building is locked, or when smoke is detected. Write an expression using W, L and S.
Show the answer
The alarm is A = W ∧ L ∨ S. The window and lock conditions must both hold, so they are joined by AND. Smoke alone is enough, so it joins by OR. AND is done first, so no brackets are needed.
Section 3
The laws of Boolean algebra
Why laws matter
Two expressions are equivalent if they give the same output on every row of the truth table. Equivalent expressions can be built as different circuits. The laws of Boolean algebra let you change one expression into an equivalent, simpler one without drawing a table.
Some laws look like ordinary algebra, and some do not. Learn them as pairs. Each law has an AND version and an OR version, made by swapping ∧ with ∨ and 0 with 1. This pattern is called duality.
The basic laws
The identity laws say A ∧ 1 = A and A ∨ 0 = A. The annulment laws say A ∧ 0 = 0 and A ∨ 1 = 1. The idempotent laws say A ∧ A = A and A ∨ A = A. The complement laws say A ∧ ¬A = 0 and A ∨ ¬A = 1.
Double negation says ¬¬A = A: inverting twice gives the original. Commutation lets you swap the order, so A ∧ B = B ∧ A. Association lets you regroup, so (A ∧ B) ∧ C = A ∧ (B ∧ C). Both work for OR too.
Common mistake: applying ordinary arithmetic to Boolean values. In Boolean algebra A ∨ A = A, not 2A, and 1 ∨ 1 = 1, not 2. There are only two values, so nothing can grow beyond 1. Treat ∨ as "at least one", never as addition.
Distribution and absorption
Distribution lets you expand brackets. AND distributes over OR, as in ordinary algebra: A ∧ (B ∨ C) = A ∧ B ∨ A ∧ C. But OR also distributes over AND: A ∨ B ∧ C = (A ∨ B) ∧ (A ∨ C). This second form has no match in arithmetic.
Absorption removes terms that add nothing. A ∨ A ∧ B = A, because whenever A ∧ B is 1, A is already 1. Its dual is A ∧ (A ∨ B) = A. Absorption is one of the most useful laws for simplifying.
Derivation: the absorption law
Start from the left-hand side and use the identity law to write A as A ∧ 1.
AND distributes over OR, so A can be taken outside a bracket.
The annulment law says 1 ∨ B = 1, whatever B is. Then the identity law finishes the proof.
So A ∨ (A ∧ B) = A for every value of A and B. A truth table confirms it: the output column matches the A column on all four rows.
De Morgan's laws
De Morgan's laws tell you how to remove a NOT from a bracket. To break the bar over an expression, invert each part and change the operator. So ¬(A ∧ B) = ¬A ∨ ¬B, and ¬(A ∨ B) = ¬A ∧ ¬B.
Read them in words. "Not both A and B" means "at least one of them is not true". "Neither A nor B" means "not A and not B". The laws also explain why NAND and NOR are universal.
Figure 17.1 lets you check De Morgan's laws one row at a time. Move through the four input rows, and compare the two circuits. Then switch on the wrong model to see what happens if the operator is not changed.
De Morgan's laws, row by row
- ¬(A ∧ B)
- 1
- ¬A ∨ ¬B
- 1
- Equivalent on every row?
- yes
Common mistake: splitting the bar without changing the operator. ¬(A ∧ B) is not ¬A ∧ ¬B. When A is 1 and B is 0, ¬(A ∧ B) = 1 but ¬A ∧ ¬B = 0. Whenever you break a bar, swap AND for OR, or OR for AND.
Duality gives a quick memory aid for all these laws. Write any law, swap every ∧ with ∨ and every 0 with 1, and the result is also a law. So learning one of each pair gives you the other for free.
De Morgan's laws extend to longer expressions. ¬(A ∧ B ∧ C) = ¬A ∨ ¬B ∨ ¬C. For a bar over a complicated bracket, apply the law to the outermost operator first, then work inwards.
Worked example 4: using De Morgan's law
Simplify ¬(¬A ∧ B).
| Working | Reason |
|---|---|
| ¬(¬A ∧ B) = ¬¬A ∨ ¬B | De Morgan: invert each part and change AND to OR |
| ¬¬A = A | Double negation cancels the two NOTs |
| Result: A ∨ ¬B | No further law makes it shorter |
| Check with A = 0, B = 1: ¬(1 ∧ 1) = 0 and 0 ∨ 0 = 0 | One row agrees; a truth table confirms all four |
Worked example 5: distributing in reverse
Simplify (A ∨ B) ∧ (A ∨ ¬B).
| Working | Reason |
|---|---|
| (A ∨ B) ∧ (A ∨ ¬B) = A ∨ (B ∧ ¬B) | OR distributes over AND, used in reverse |
| B ∧ ¬B = 0 | Complement law: B cannot be both 1 and 0 |
| A ∨ 0 = A | Identity law for OR |
| Result: A | The value of B makes no difference at all |
This result surprises many students. Expanding the brackets the long way gives the same answer, but takes four steps more. Spotting a common term is the quickest route.
Proof by truth table
Algebra is one way to prove that two expressions are equivalent. A truth table is the other. Build a column for each expression, row by row. If the two columns match on every row, the expressions are equivalent. If even one row differs, they are not.
A truth table proof is mechanical, so it is hard to get wrong. But it grows quickly: four inputs need sixteen rows. Use algebra when you can see the steps, and a table when you need certainty or a check.
Check yourself. Use De Morgan's law to write ¬(A ∨ ¬B) without a bar over a bracket.
Show the answer
Invert each part and change OR to AND: ¬A ∧ ¬¬B. Double negation turns ¬¬B into B, so the result is ¬A ∧ B.
Section 4
Simplifying expressions
Why simplify?
A simpler expression needs fewer gates. Fewer gates make a chip cheaper and smaller. They also use less power and give a faster result, because signals pass through fewer stages. In code, a simpler condition is easier to read and test.
A good measure of size is the number of gates. The expression A ∧ B ∨ A ∧ ¬B ∨ ¬A ∧ B needs seven gates: three ANDs, two ORs and two NOTs. You will see that it simplifies to a single OR gate.
A method for simplifying
Follow a routine. First remove any bar over a bracket with De Morgan's laws. Then cancel double negations. Next look for a common factor to take outside a bracket. Finally use the complement, identity and absorption laws to remove terms.
Write one step per line and name the law. This is how exam mark schemes award credit. A correct final answer with no working may score only one mark out of four.
Worked example 6: from seven gates to one
Simplify A ∧ B ∨ A ∧ ¬B ∨ ¬A ∧ B.
| Working | Reason |
|---|---|
| A ∧ (B ∨ ¬B) ∨ ¬A ∧ B | Distribution: take A out of the first two terms |
| A ∧ 1 ∨ ¬A ∧ B = A ∨ ¬A ∧ B | Complement law, then the identity law |
| (A ∨ ¬A) ∧ (A ∨ B) | OR distributes over AND |
| 1 ∧ (A ∨ B) = A ∨ B | Complement law, then the identity law |
The final step used a useful pattern: A ∨ ¬A ∧ B = A ∨ B. In words, if A is false then B must be true, which is the same as "A or B". Learn this pattern; it appears often.
Worked example 7: De Morgan first, then expand
Simplify ¬(A ∧ ¬B) ∧ (A ∨ B).
| Working | Reason |
|---|---|
| (¬A ∨ B) ∧ (A ∨ B) | De Morgan and double negation on the first bracket |
| (¬A ∧ A) ∨ (¬A ∧ B) ∨ (B ∧ A) ∨ (B ∧ B) | Distribution: multiply out the two brackets |
| 0 ∨ ¬A ∧ B ∨ A ∧ B ∨ B | Complement law, and the idempotent law B ∧ B = B |
| Result: B | Absorption: B absorbs both terms that contain B |
There is a quicker route here too. Both brackets contain B, so (¬A ∨ B) ∧ (A ∨ B) = B ∨ (¬A ∧ A) = B ∨ 0 = B. Either route earns full marks if each step is valid.
Checking your answer
Always check a simplification. Choose two or three rows and evaluate both expressions. If any row disagrees, there is an error. For a full check, complete a truth table for each: equivalent expressions have identical output columns.
Common mistake: stopping before the expression is fully simplified. A ∨ ¬A ∧ B looks finished, but it simplifies to A ∨ B. Before you stop, check every term for a common factor, a complement pair or a term that another absorbs.
Choosing between algebra and a map
Algebra is flexible and shows every step, which exams often require. But it relies on spotting the right law, and it is easy to miss a shortcut. For expressions of three or four inputs, a Karnaugh map, in section 5, is usually faster and more reliable.
For more than four inputs, maps become hard to draw. Engineers then use computer tools that apply the same ideas automatically. The Quine-McCluskey method, for instance, finds all the largest groups systematically, just as you do by eye on a map.
Simplifying conditions in code
The same laws tidy program conditions. A condition such as not (logged_in and not banned) becomes not logged_in or banned by De Morgan's law. The second form is easier to read, and it makes clear which case the IF statement is catching.
Check yourself. Using the laws of Boolean algebra, reduce ¬(¬A ∨ B) ∨ A ∧ B to a single input, naming each law.
Show the answer
De Morgan and double negation give A ∧ ¬B ∨ A ∧ B. Distribution gives A ∧ (¬B ∨ B). The complement law makes the bracket 1, and the identity law leaves A.
Section 5
Karnaugh maps
A picture of a truth table
Algebra needs you to spot which law to use next. A Karnaugh map, or K-map, makes that easier by drawing the truth table as a grid. Cells whose inputs differ by just one bit sit side by side. Then any pair of neighbouring 1s can be combined.
A two-input map has two rows and two columns. A three-input map has two rows and four columns. A four-input map is a 4 × 4 grid, with two inputs labelling the rows and two the columns.
Gray code order
The row and column labels are not in binary order. They follow Gray code: 00, 01, 11, 10. Each label differs from the one beside it in exactly one bit. This is what makes neighbouring cells combinable.
Common mistake: labelling Karnaugh map columns in binary order. Writing 00, 01, 10, 11 puts 01 next to 10, which differ in two bits. Groups drawn across them are wrong. Always use Gray code, 00, 01, 11, 10, so that neighbours differ in one bit.
The grouping rules
Draw rectangles around the 1s, following five rules. Each group must contain only 1s. Its size must be a power of two: 1, 2, 4, 8 or 16. Groups must be rectangles, never diagonal or L-shaped. Make each group as large as possible. Every 1 must be in at least one group.
Groups may overlap if that makes them larger. Use as few groups as you can. Each group becomes one AND term, and the terms are joined with OR.
Common mistake: making groups that are not a power of two. A group of three 1s cannot be written as a single term, because three cells do not fix any whole number of inputs. Split it into two overlapping pairs instead.
Why grouping works
A group is the complement law in picture form. Two neighbouring cells differ in one input, say C. Their terms are X ∧ C and X ∧ ¬C, for some common part X. Distribution gives X ∧ (C ∨ ¬C), and the complement law turns that into X. The changing input disappears.
A group of four repeats the trick twice, removing two inputs. So every group on a map is a short algebraic proof that you have done by eye. That is why the answer from a correct map is always equivalent to the original.
Reading a group
To write the term for a group, look at each input in turn. If the input is 1 across the whole group, include it. If it is 0 across the whole group, include it inverted. If it changes inside the group, leave it out, because it does not matter.
A pair removes one input, a group of four removes two, and a group of eight removes three. Bigger groups mean shorter terms. That is why the rule says to make groups as large as possible.
Worked example 8: a three-input map
Simplify Q = ¬A ∧ ¬B ∧ C ∨ ¬A ∧ B ∧ C ∨ A ∧ B ∧ C ∨ A ∧ B ∧ ¬C.
| Working | Reason |
|---|---|
| Place 1s at ABC = 001, 011, 111 and 110 | One cell for each term of the expression |
| Row A = 1, columns 11 and 10: A ∧ B | A and B stay 1 while C changes |
| Row A = 0, columns 01 and 11: ¬A ∧ C | A stays 0 and C stays 1 while B changes |
| Q = A ∧ B ∨ ¬A ∧ C | Join the group terms with OR |
The cell 111 is in both groups. That is allowed, and it let each group be a pair rather than a single cell. Twelve literals have become four.
Wrapping round the edges
A K-map wraps round. The left column is a neighbour of the right column, because 00 and 10 differ in one bit. The top row is a neighbour of the bottom row in a four-input map. The four corners form a single group.
Common mistake: forgetting that groups wrap around the edges. The leftmost and rightmost columns differ in only one bit, so 1s in them can be grouped together. Missing this leaves extra terms, so the answer is correct but not fully simplified.
Worked example 9: a four-input map with corners
Q is 1 on rows 0, 2, 8, 10, 11 and 15 of the truth table for A, B, C and D. Find the simplest expression.
| Working | Reason |
|---|---|
| Rows 0, 2, 8, 10 are the four corners | Corners are neighbours because the map wraps |
| Corners: B = 0 and D = 0 throughout, giving ¬B ∧ ¬D | A and C change, so both drop out |
| Rows 11 and 15: A = 1, C = 1, D = 1, giving A ∧ C ∧ D | Only B changes in this vertical pair |
| Q = ¬B ∧ ¬D ∨ A ∧ C ∧ D | Every 1 is now in a group |
Row 10 could also join row 11 as a pair. But it is already covered by the corner group, so that extra term would add nothing. Use as few groups as you can.
Check yourself. In a four-input map, how many inputs remain in the term for a group of eight cells?
Show the answer
One input remains. A group of eight is 2³ cells, so three inputs change inside it and drop out. Only the one input that stays constant across the group is left.
Section 6
Adders and flip-flops
Adding two bits: the half adder
Binary addition of two single bits has four cases. 0 + 0 = 0, 0 + 1 = 1 and 1 + 0 = 1. But 1 + 1 = 10 in binary: a sum bit of 0 and a carry of 1.
So adding two bits needs two outputs. The sum S is 1 when exactly one input is 1, which is A ⊻ B. The carry C is 1 only when both are 1, which is A ∧ B. One XOR gate and one AND gate make a half adder.
Adding a column: the full adder
When adding longer numbers, each column after the first also receives a carry from the column to its right. So it must add three bits: A, B and the carry in. A circuit that adds three bits is a full adder.
Two half adders make a full adder. The first adds A and B. The second adds that sum to the carry in, giving the final sum S = A ⊻ B ⊻ Cin. An OR gate combines the two carries to give the carry out.
Derivation: the carry out of a full adder
The carry out is 1 when at least two of the three inputs are 1. The two half adders produce it in two parts.
The first half adder gives A ∧ B. The second gives a carry only when A ⊻ B and Cin are both 1. Grouping the 1s of the carry column on a Karnaugh map gives an equivalent, symmetric form.
Both forms give 1 on exactly the rows 011, 101, 110 and 111. The first reuses the half adders' gates. The second shows clearly that the carry is the majority of the three inputs.
Common mistake: ignoring the carry between columns. Adding each column on its own gives A ⊻ B, which is wrong whenever a column holds two 1s. Every column except the first must add the carry from the column to its right, which is why full adders are needed.
The ripple-carry adder
To add two 4-bit numbers, chain four full adders. The carry out of each feeds the carry in of the next, from the least significant bit to the most. The carry ripples along the chain, giving the circuit its name.
Figure 17.2 shows a 4-bit ripple-carry adder. Choose two numbers and watch the carries pass from right to left. Then switch on the wrong model to see what happens if the carries are ignored.
A 4-bit ripple-carry adder
- 4-bit sum
- 0001 (1)
- Carry out
- 1
- Overflow?
- yes: the answer needs 5 bits
If the top adder produces a carry out of 1, the answer needs five bits. In a 4-bit register that is an overflow. The processor sets a flag so that a program can detect it.
The ripple is also the adder's weakness. Each full adder must wait for the carry from its neighbour. A 64-bit adder could wait for 64 stages. Real processors use carry-lookahead circuits that work out the carries in parallel.
Worked example 10: adding with a ripple-carry adder
A 4-bit ripple-carry adder adds A = 1011 and B = 0110. Find the sum bits and the carry out.
| Working | Reason |
|---|---|
| Column 0: 1 + 0 + 0 = 1, carry 0 | Least significant bits, with no carry in |
| Column 1: 1 + 1 + 0 = 10, so sum 0, carry 1 | Two 1s give sum 0 and a carry |
| Column 2: 0 + 1 + 1 = 10, so sum 0, carry 1 | The carry from column 1 is added in |
| Column 3: 1 + 0 + 1 = 10, so sum 0, carry out 1 | The top column passes its carry out of the adder |
| Sum bits 0001 with carry out 1, so 11 + 6 = 17 | The 4-bit result has overflowed into a fifth bit |
Storing a bit: the D-type flip-flop
Adders give an output that depends only on their inputs now. Memory needs a circuit whose output depends on the past. A flip-flop stores one bit, and it holds that bit until it is told to change.
An edge-triggered D-type flip-flop has a data input D, a clock input and an output Q. On each rising edge of the clock, the moment it changes from 0 to 1, Q takes the value of D. At every other time Q stays the same, whatever D does.
Common mistake: treating a flip-flop as level-triggered. Q does not follow D whenever the clock is 1. It changes only at the instant of the rising edge. Changes to D between edges have no effect until the next rising edge arrives.
Inside the arithmetic logic unit
The arithmetic logic unit, or ALU, is the part of the processor that calculates. Its adder is a chain of full adders like the one in Figure 17.2. Other gates beside it compute AND, OR and XOR of whole registers, one pair of bits at a time.
Subtraction uses the same adder. To find A − B, the processor inverts every bit of B and sets the first carry in to 1. This adds the two's complement of B. A single circuit can therefore add and subtract, chosen by one control signal.
Flip-flops make registers. Eight flip-flops sharing one clock form an 8-bit register. Registers inside the processor, such as the accumulator and the program counter, are built this way. The clock keeps every part of the processor in step.
Check yourself. Which two outputs does a half adder produce for the inputs 1 and 1, and which gates compute them?
Show the answer
It produces a sum of 0 and a carry of 1, because 1 + 1 = 10 in binary. The sum comes from an XOR gate and the carry from an AND gate.
Reference
Key algorithms and definitions
| Term or algorithm | What it means | Example | Cost or rule to learn |
|---|---|---|---|
| Truth table | Every input combination with its output | Two inputs give four rows | n inputs need 2ⁿ rows |
| AND, OR, NOT | Both; at least one; the opposite | 1 ∧ 0 = 0, 1 ∨ 0 = 1, ¬1 = 0 | NOT first, then AND, then OR |
| XOR | Exactly one input is 1 | 1 ⊻ 1 = 0 | Used for the sum bit of an adder |
| NAND and NOR | AND or OR followed by NOT | ¬(A ∧ B) | Each alone can build every gate |
| De Morgan's laws | Break a bar: invert each part, swap the operator | ¬(A ∧ B) = ¬A ∨ ¬B | Always change the operator |
| Distribution | Expand or factorise brackets | A ∨ B ∧ C = (A ∨ B) ∧ (A ∨ C) | Works both ways, for AND and OR |
| Absorption | A term absorbs any term containing it | A ∨ A ∧ B = A | Also A ∧ (A ∨ B) = A |
| Karnaugh map | A truth table as a grid in Gray code | Columns 00, 01, 11, 10 | Groups of 1, 2, 4, 8 or 16 that may wrap |
| Half adder | Adds two bits | S = A ⊻ B, C = A ∧ B | One XOR and one AND gate |
| Full adder | Adds two bits and a carry in | S = A ⊻ B ⊻ Cin | Two half adders and an OR gate |
| D-type flip-flop | Stores one bit | Q takes D at a rising clock edge | Holds its value between edges |
Programming task
Programming task: a truth table generator
Aim
Write a program that prints the truth table of any three-input Boolean function, and use it to check whether two expressions are equivalent. Exhaustive testing like this is how engineers confirm that a simplified circuit is correct.
Design
A function takes the three inputs as 0 or 1 and returns the output. Three nested loops, or one loop over the numbers 0 to 7, generate the eight rows in binary order. For each row the program prints the inputs and the output.
To test equivalence, the program builds the output column of each function as a string. Two functions are equivalent exactly when their strings match. The string also gives a compact answer to compare with a hand-written table.
Code
def table(f):
"""Return the output column of a three-input function as a string, rows 000 to 111."""
column = ""
for row in range(8):
a = (row >> 2) & 1
b = (row >> 1) & 1
c = row & 1
column += str(int(f(a, b, c)))
return column
def print_table(f):
print("A B C | Q")
for row in range(8):
a, b, c = (row >> 2) & 1, (row >> 1) & 1, row & 1
print(a, b, c, "|", int(f(a, b, c)))
def equivalent(f, g):
return table(f) == table(g)
original = lambda a, b, c: (a and b) or (a and not b) or ((not a) and b)
simplified = lambda a, b, c: a or b
print_table(original)
print("Equivalent:", equivalent(original, simplified))
The >> operator shifts the row number right, and & 1 keeps only the last bit. So row 5, which is 101 in binary, gives a = 1, b = 0 and c = 1. Using int() turns Python's True and False into 1 and 0.
Testing
| Test | Input | Expected output | Why this test |
|---|---|---|---|
| 1 | f = a and not b | 00001100 | A two-input function inside a three-input table |
| 2 | f = (a != b) and c | 00010100 | XOR written with != matches worked example 1 |
| 3 | f = not (a and b) | 11111100 | A NAND gate, to test the NOT on a bracket |
| 4 | f = (not a) or (not b) | 11111100 | De Morgan: must match test 3 exactly |
| 5 | original and simplified | Equivalent: True | The simplification from worked example 6 |
| 6 | f = not a and not b, g = not (a and b) | Equivalent: False | The split-bar mistake must be caught |
| 7 | f = a or not a | 11111111 | A function that is always true |
Evaluation
The program checks all eight rows, so it proves equivalence for three inputs, not just suggests it. Exhaustive testing grows quickly, though: n inputs need 2ⁿ rows, so 30 inputs would need over a billion. For large circuits, engineers combine testing with algebraic proof.
An improvement would accept any number of inputs, using the length of the input list to set the number of rows. Another would print the rows where two functions disagree, which helps to find a mistake in a simplification.
Practice
Practice ladder
Rung 1: Recall and fluency
Q1. State the output of an XOR gate for each of the four input combinations of A and B.
Worked solution to Q1
XOR gives 1 when the inputs differ. So the outputs for 00, 01, 10 and 11 are 0, 1, 1 and 0. Only the last row differs from OR, which would give 1.
Q2. Write down both of De Morgan's laws for two inputs, A and B.
Worked solution to Q2
The first law is ¬(A ∧ B) = ¬A ∨ ¬B. The second is ¬(A ∨ B) = ¬A ∧ ¬B. In each, every part is inverted and the operator is swapped.
Q3. Give the order of the labels on one side of a four-input Karnaugh map, and explain why that order is used.
Worked solution to Q3
The labels go 00, 01, 11, 10, which is Gray code. Each label differs from its neighbour in one bit. This means neighbouring cells can always be combined into a shorter term.
Rung 2: Application
Q4. Complete the output column of the truth table for Q = ¬(A ∨ B) ∧ C, rows 000 to 111.
Worked solution to Q4
¬(A ∨ B) is 1 only when A and B are both 0, which is rows 000 and 001. ANDing with C keeps only row 001. So the output column is 01000000.
Q5. A door unlocks (U) if a correct code is entered (C) and the alarm is not set (S), or if a fire is detected (F). Write a Boolean expression for U.
Worked solution to Q5
The code and alarm conditions must hold together, so they are joined by AND: C ∧ ¬S. A fire alone unlocks the door, so it joins by OR. This gives U = C ∧ ¬S ∨ F, with AND done first.
Q6. Show that (A ∨ B) ∧ ¬(A ∧ B) describes a single gate, and name the gate.
Worked solution to Q6
De Morgan gives (A ∨ B) ∧ (¬A ∨ ¬B). Expanding gives A ∧ ¬A ∨ A ∧ ¬B ∨ B ∧ ¬A ∨ B ∧ ¬B. The complement terms vanish, leaving A ∧ ¬B ∨ ¬A ∧ B. This is 1 when the inputs differ, which is an XOR gate.
Q7. Using A ∨ ¬A ∧ B = A ∨ B, simplify P ∨ ¬P ∧ (Q ∧ R).
Worked solution to Q7
Treat Q ∧ R as a single input in the pattern A ∨ ¬A ∧ B = A ∨ B. Then P ∨ ¬P ∧ (Q ∧ R) = P ∨ Q ∧ R. This is shorter by one literal and one NOT gate.
Rung 3: Analysis
Q8. Q is 1 on rows 0, 1, 4, 5, 10 and 14 of a four-input truth table for A, B, C and D. Use a Karnaugh map to find the simplest expression.
Worked solution to Q8
Rows 0, 1, 4 and 5 form a square in rows AB = 00 and 01, columns CD = 00 and 01. There A = 0 and C = 0, giving ¬A ∧ ¬C. Rows 10 and 14 form a vertical pair in column CD = 10, with A = 1, C = 1 and D = 0. So Q = ¬A ∧ ¬C ∨ A ∧ C ∧ ¬D.
Q9. A student simplifies ¬(A ∧ B) ∨ A to ¬A ∧ ¬B ∨ A. Identify the error and give the correct simplest form.
Worked solution to Q9
The student split the bar without changing the operator. Correctly, ¬(A ∧ B) = ¬A ∨ ¬B, so the expression is ¬A ∨ ¬B ∨ A. Since ¬A ∨ A = 1, the whole expression is 1: it is always true.
Q10. Add 0111 and 0101 with a 4-bit ripple-carry adder. Give the carry into each column, the sum bits and the carry out.
Worked solution to Q10
Column 0: 1 + 1 = 10, sum 0, carry 1. Column 1: 1 + 0 + 1 = 10, sum 0, carry 1. Column 2: 1 + 1 + 1 = 11, sum 1, carry 1. Column 3: 0 + 0 + 1 = 1, carry 0. The carries in are 0, 1, 1, 1, the sum is 1100 and the carry out is 0, so 7 + 5 = 12 with no overflow.
Rung 4: Stretch
Q11. Show how to make an AND gate using only NOR gates, and prove it with De Morgan's law.
Worked solution to Q11
Make ¬A and ¬B by feeding each input into both inputs of its own NOR gate. Then a third NOR gives ¬(¬A ∨ ¬B). By De Morgan, this is ¬¬A ∧ ¬¬B, which double negation turns into A ∧ B. Three NOR gates make one AND gate.
Q12. A D-type flip-flop starts with Q = 0. Its clock samples are 0, 1, 1, 0, 1, 0, 1 and its D samples are 1, 0, 1, 1, 0, 0, 1. Give Q after each sample.
Worked solution to Q12
Rising edges happen at samples 2, 5 and 7, where the clock goes from 0 to 1. At those moments Q copies D, which is 0, 0 and 1. So Q is 0, 0, 0, 0, 0, 0, 1. The D values between edges have no effect.
Q13. Explain why a 32-bit ripple-carry adder is slower than an 8-bit one, and estimate how much slower if each stage takes two gate delays.
Worked solution to Q13
Each full adder must wait for the carry from the stage before it, so the delay grows with the number of bits. An 8-bit adder needs about 8 × 2 = 16 gate delays and a 32-bit adder about 64. The 32-bit adder is about four times slower.
Exam practice
Exam technique and worked answer workshop
Boolean questions reward clear, step-by-step working. When simplifying, write one law per line and name it, such as "De Morgan" or "absorption". Mark schemes give method marks for each correct step, even if a slip spoils the final answer.
When completing truth tables, add a column for each bracket or gate. Count the rows before you start, and list them in binary order. For Karnaugh maps, write the Gray code labels first, then draw each group and write its term beside it.
In extended answers about circuits, use precise vocabulary. Say "half adder", "carry in", "carry out" and "overflow". Explain how the parts connect, not just what each part is. Diagrams can help, but label every gate and wire you draw.
Exam question 1 (4 marks · Paper 01 · about 5 minutes)
A circuit has inputs A, B and C, and output Q = ¬A ∧ (B ∨ C).
(a) Complete the output column of the truth table for Q, for the rows 000 to 111. [2]
(b) State the number of gates needed to build the circuit exactly as written. [1]
(c) Give the row with A = 0 for which Q = 0. [1]
Mark scheme for exam question 1
- B1 rows 000 to 011 correct: 0, 1, 1, 1
- B1 rows 100 to 111 all 0
- B1 three gates: one NOT, one OR and one AND
- B1 row 000, where B and C are both 0
Exam question 2 (5 marks · Paper 01 · about 6 minutes)
Simplify the expression ¬(¬X ∨ Y) ∨ X ∧ Y. Show your working and name each law you use.
Mark scheme for exam question 2
- M1 De Morgan's law applied: ¬¬X ∧ ¬Y
- A1 double negation: X ∧ ¬Y ∨ X ∧ Y
- M1 distribution: X ∧ (¬Y ∨ Y)
- A1 complement law: X ∧ 1
- A1 identity law: X
Exam question 3 (5 marks · Paper 01 · about 7 minutes)
A function of four inputs P, Q, R and S is 1 on rows 1, 3, 5, 7, 12 and 13 of its truth table. It is 0 on every other row.
(a) Draw a Karnaugh map for the function, labelling the rows PQ and the columns RS. [2]
(b) Use your map to write the simplest sum-of-products expression. [3]
Mark scheme for exam question 3
- B1 rows and columns labelled in Gray code order 00, 01, 11, 10
- B1 1s placed correctly in all six cells
- M1 a group of four formed from rows 1, 3, 5 and 7
- A1 that group written as ¬P ∧ S
- A1 the pair at rows 12 and 13 written as P ∧ Q ∧ ¬R, giving ¬P ∧ S ∨ P ∧ Q ∧ ¬R
Exam question 4 (6 marks · Paper 01 · about 9 minutes)
Explain how a computer adds two 8-bit binary numbers using logic gates. Your answer should describe half adders, full adders and how they are combined, and explain how overflow is detected.
Mark scheme for exam question 4
Level 3 (5–6 marks): a clear explanation of the half adder and the full adder, with their gates and outputs. It explains how eight full adders are chained, each carry out feeding the next carry in. Overflow detection is explained. The answer is well organised and uses technical terms accurately.
Level 2 (3–4 marks): a correct description of adders with some detail of gates or carries, but the chaining or the overflow is incomplete or unclear.
Level 1 (1–2 marks): some relevant points, such as naming an adder or stating that 1 + 1 gives a carry, with little explanation. The answer may lack structure.
0 marks: no response or no response worthy of credit.
Indicative content:
- a half adder adds two bits and has two outputs, sum and carry
- sum = A ⊻ B, from an XOR gate; carry = A ∧ B, from an AND gate
- a full adder also adds a carry in, so it adds three bits
- a full adder can be made from two half adders and an OR gate
- eight full adders are chained, least significant bit first
- each carry out feeds the carry in of the next column: a ripple-carry adder
- a carry out of the most significant adder means the result needs nine bits
- this overflow sets a flag in the status register, so software can detect it
Annotated model answer: exam question 4
"A half adder adds two bits, A and B. It has two outputs: the sum, from an XOR gate, and the carry, from an AND gate. For 1 + 1, the sum is 0 and the carry is 1." (Half adder with its gates and a worked case.)
"When adding longer numbers, each column must also add the carry from the column before. A full adder adds three bits, A, B and the carry in. It is made from two half adders, with an OR gate combining their carries into a carry out." (Why a full adder is needed and how it is built.)
"To add two 8-bit numbers, eight full adders are connected in a chain. The least significant bits go into the first adder, whose carry in is 0. Each adder's carry out feeds the carry in of the next, so the carry ripples along." (The ripple-carry adder.)
"If the last adder gives a carry out of 1, the answer is too big for eight bits. This overflow sets a flag in the status register, which a program can test." (Overflow detection.)
This answer reaches Level 3 and scores 6 marks. It explains how the parts connect, not just what they are called.
Exam question 5 (4 marks · Paper 01 · about 5 minutes)
(a) Describe the behaviour of an edge-triggered D-type flip-flop. [2]
(b) A flip-flop's clock has rising edges at times 1, 3 and 5. The D input is 1 at time 1, 1 at time 3 and 0 at time 5. Q starts at 0. State Q just after time 5, and explain why a change of D at time 4 has no effect. [2]
Mark scheme for exam question 5
- B1 on a rising edge of the clock, Q takes the value of D
- B1 at all other times Q holds its value, so it stores one bit
- B1 Q = 0 just after time 5
- B1 at time 4 there is no rising edge, so Q cannot change until the edge at time 5
Exam question 6 (6 marks · Paper 02 · about 9 minutes)
Write a function fullAdder(a, b, carryIn) that returns the sum bit and the carry out, using only Boolean operators. Then write a function addBits(x, y) that adds two lists of four bits. The least significant bit is at index 0. The function returns the four sum bits and the final carry.
Mark scheme for exam question 6
- B1 sum computed as a XOR b XOR carryIn
- B1 carry out computed as (a AND b) OR (carryIn AND (a XOR b)), or an equivalent
- B1 both values returned from fullAdder
- M1 a loop over the four bit positions, starting from index 0
- M1 each call passes the carry from the previous position as carryIn, starting at 0
- A1 the four sum bits and the final carry are returned
Annotated model answer: exam question 6
function fullAdder(a, b, carryIn)
s = a XOR b XOR carryIn
carryOut = (a AND b) OR (carryIn AND (a XOR b))
return s, carryOut
endfunction
function addBits(x, y)
result = [0, 0, 0, 0]
carry = 0
for i = 0 to 3
result[i], carry = fullAdder(x[i], y[i], carry)
next i
return result, carry
endfunction
The first function earns the three B marks: correct sum, correct carry, and both returned. The loop starts from index 0, the least significant bit, which earns the first M mark. Passing carry back into the next call is the ripple, which earns the second. Returning both the list and the carry earns the A mark.
Exam question 7 (5 marks · Paper 01 · about 7 minutes)
A lift moves (M) when its door is closed (D) and a floor button is pressed (F), unless the lift is overloaded (O). An engineer's switch (E) moves the lift whatever the other inputs are.
(a) Write a Boolean expression for M. [2]
(b) Draw a logic circuit for your expression, and state how many gates it uses. [3]
Mark scheme for exam question 7
- M1 door, button and not overloaded joined by AND: D ∧ F ∧ ¬O
- A1 M = D ∧ F ∧ ¬O ∨ E
- B1 a NOT gate on input O
- B1 AND gates combining D, F and ¬O, and an OR gate adding E
- B1 four gates in total: one NOT, two two-input ANDs and one OR
Exam question 8 (5 marks · Paper 02 · about 7 minutes)
A programmer claims that the condition not (x > 0 and y > 0) can be replaced by x <= 0 or y <= 0.
(a) Use De Morgan's law to justify the claim. [2]
(b) Explain how exhaustive testing could confirm that two Boolean conditions of three inputs are equivalent, and state how many test cases are needed. [3]
Mark scheme for exam question 8
- B1 De Morgan: not (P and Q) = (not P) or (not Q)
- B1 not (x > 0) is x <= 0, and not (y > 0) is y <= 0
- M1 evaluate both conditions for every combination of the inputs' truth values
- A1 2³ = 8 test cases
- B1 the conditions are equivalent only if the outputs match on every case
Connections
Synoptic link
Logic gates are where hardware and software meet. The adders in this chapter sit inside the arithmetic logic unit, described in Processor architecture and performance, and the flip-flops form its registers. Binary addition and overflow come from Number systems and binary arithmetic.
On the software side, every condition in Programming constructs is a Boolean expression. Simplifying a condition uses De Morgan's law exactly as a circuit does. Exhaustive testing of conditions links to Development environments, testing and computational methods.
Synoptic question 1. An 8-bit ripple-carry adder adds the unsigned numbers 200 and 100. State the 8-bit result and the carry out, and explain what a program should do next.
Answer to synoptic question 1
The true total is 300, which needs nine bits. The adder keeps the low eight bits, 300 − 256 = 44, and gives a carry out of 1. The carry flag shows an overflow, so the program should report an error or use a larger register.
Recap
Recap and next step
- Boolean logic has two values, 1 and 0, and three basic operations: AND, OR and NOT; XOR, NAND and NOR are built from them.
- A truth table lists all 2ⁿ input combinations in binary order; precedence is NOT, then AND, then OR.
- The laws of Boolean algebra come in dual pairs; De Morgan's laws invert each part and swap the operator.
- Simplify step by step, naming each law, and check with a truth table or a few test rows.
- Karnaugh maps use Gray code; groups are rectangles of 1, 2, 4, 8 or 16 cells, as large as possible, and may wrap.
- A half adder gives sum A ⊻ B and carry A ∧ B; full adders chained in a ripple-carry adder add whole numbers and detect overflow.
- A D-type flip-flop stores one bit, copying D to Q only at a rising clock edge.
Next chapter: Legal, moral, ethical and cultural issues turns from how computers work to how they affect people and society.
For your exam board
OCR A Level Computer Science (H446): this chapter covers section 1.4.3, Boolean algebra, in Component 01, Computer systems. It includes defining problems with Boolean logic, the laws of Boolean algebra, Karnaugh maps, logic gate diagrams and truth tables, D-type flip-flops, and half and full adders.
AQA A Level Computer Science (7517): logic gates, Boolean algebra, adders and the edge-triggered D-type flip-flop appear in the fundamentals of computer systems section, assessed on Paper 2. Karnaugh maps are not required by AQA.