← AS/A Level

CS · Chapter 19

Cambridge AS & A Level · Part 3 · Chapter 19

Logic Circuits and Boolean Algebra

How gates add numbers and remember bits, and how a little algebra (or a Karnaugh map) turns a messy truth table into the simplest possible expression. Every figure, task, question and exam question from the chapter is here, with live labs for each idea.

Guru Jazzy
Chapter 19 opener: tracks and pads on a circuit board

🎯 Learning objectives

By the end of this chapter you should be able to:

  • produce truth tables for logic circuits, including half adders and full adders
  • show understanding of a flip-flop (SR, JK)
  • Show understanding of Boolean algebra
  • Show understanding of Karnaugh maps

➕ 19.01 Logic circuits: the half adder

Chapter 4 introduced the gate symbols and how circuits, truth tables and logic expressions relate. This chapter uses them to build circuits that do real jobs inside the hardware.

Adding two bits is the most basic operation in computing. The result is 0 or 1, but 1 + 1 gives 0 with a carry of 1, and that carry cannot be ignored when the numbers have several bits.

The simplest adding circuit is the half adder. It takes two input bits and outputs a sum bit S and a carry bit C.

Figure 19.01A half adder: two input bits A and B, outputs a sum bit S and a carry bit C
InputOutput
ABSC
0000
0110
1010
1101

Table 19.01 The truth table for a half adder

Look at the columns: C is 1 only when both inputs are 1, which is the AND truth table. S matches XOR (Chapter 4, Section 4.04). So one half adder is simply an AND gate and an XOR gate, each fed from A and B.

That is only one design. NAND and NOR are universal gates: any circuit can be built from NAND gates alone, or NOR gates alone. They are also easy to manufacture, so chip makers prefer them. Figure 19.02 is a half adder made only of NAND gates.

Figure 19.02A half adder circuit constructed from NAND gates, with intermediate points W, X and Y

🔬 Lab: probe the NAND half adder

Toggle A and B. Every point in Figure 19.02 lights up when it is 1, and the matching row of Table 19.01 is highlighted.

A B
ABWXYSC
Question 19.01 In Figure 19.02, can you identify the individual circuits that represent the AND operator and the XOR operator?
Show answer

AND (the carry): the first NAND gate (output W) followed by the bottom-right NAND gate, whose two inputs are both joined to W. A NAND with its inputs tied together is a NOT gate, so C = NOT(NOT(A.B)) = A.B.

XOR (the sum): the four NAND gates that produce W, X, Y and S. W feeds the gates making X and Y, and S = NAND(X, Y).

Task 19.01 Use the intermediate points W, X and Y to construct a truth table for the circuit in Figure 19.02. Check that it reproduces Table 19.01.

Tap each cell to cycle ? → 0 → 1, then check.

ABWXYSC

➕ The full adder

To add two multi-bit binary numbers you start with the two least significant bits and work towards the most significant. At each stage the carry from the previous stage must be added in too. A half adder only takes two inputs, so it would need extra circuitry for the carry.

The full adder has three inputs: A, B and the carry in from the previous addition, Cin.

InputOutput
ABCinSCout
00000
00110
01010
01101
10010
10101
11001
11111

Table 19.02 The truth table for a full adder

One way to build it uses two half adders and an OR gate:

Figure 19.03A possible implementation of a full adder: two half adders and an OR gate
Half adder circuit: a circuit which performs binary addition of two individual bits.
Full adder circuit: a circuit which performs binary addition of two individual bits and an input carry bit.

As before, the whole circuit can be made from NAND gates:

Figure 19.04A full adder circuit constructed from NAND gates

🔬 Lab: inside the full adder (Figure 19.03)

Set A, B and Cin. Watch the first half adder, the second half adder and the OR gate.

A B Cin
0
S
0
Cout

Discussion Point: Can you see how full adders could be combined to handle the addition of, for example, four-bit binary numbers? What happens to the carry input for the first addition?
My answer

Chain four full adders, one per bit position. The Cout of each adder is wired to the Cin of the next, more significant adder: a ripple-carry adder. The first (least significant) adder has no previous carry, so its Cin is fixed at 0 (or a half adder is used for that bit). The Cout of the last adder is the fifth result bit; if only four bits are kept, a 1 there means overflow. Try it in the lab below.

🔗 Lab: a 4-bit ripple-carry adder

Toggle the bits of the two numbers. Each column is one full adder; the carry ripples from right to left.

🔁 19.02 Sequential logic circuits

Every circuit in this book so far has been a combinational circuit: its output depends only on the input values. In a sequential circuit the output depends on the inputs and on the previous output. That feedback is what lets a circuit remember.

Combinational circuit: a circuit in which the output is dependent only on the input values.
Sequential circuit: a circuit in which the output depends on the input values and the previous output.

🔒 The SR flip-flop

The SR flip-flop, or latch, is a simple sequential circuit. It can be built with two NAND gates or two NOR gates. Figure 19.05 shows the NOR version.

Figure 19.05A circuit for an SR flip-flop using NOR gates

The two gates are cross-coupled: each output feeds back into the other gate. This has interesting consequences.

  • Suppose R, S, Q and Q′ were all 0. That contradicts itself: a NOR gate with two 0 inputs outputs 1.
  • Q and Q′ both 1 is also impossible, because a NOR gate outputs 0 if either input is 1.
  • So the flip-flop is a two-state device: either Q = 1 and Q′ = 0, or the reverse.

The two rows of Table 19.03 where S and R are both 0 mean no signal is being input. Take the state Q = 1, Q′ = 0 with S = R = 0:

  • the top NOR gate has inputs R = 0 and Q′ = 0, so it outputs 1 (Q stays 1);
  • the bottom NOR gate has inputs S = 0 and Q = 1, so it outputs 0 (Q′ stays 0).

The state is self-consistent: it is the set state. The same argument works for the unset state, Q = 0 and Q′ = 1. S and R stand for set and reset: S = 0, R = 1 turns a set state into an unset state, and S = 1, R = 0 turns an unset state into a set state.

Input signalsInitial stateFinal state
SRQQ′QQ′
001010
101010
011001
000101
100110
010101

Table 19.03 A representation of a truth table for an SR flip-flop

This is why an SR flip-flop can store 1 bit, and so could be a component of RAM: a value is held, but it can be changed. There are no rows for S = 1 and R = 1, because that leads to an invalid state with Q and Q′ both 0. The circuit must be protected from receiving a signal on R and S at the same time.

The NAND gate version has a similar structure, but the labelling is different: setting is done with S = 0 and R = 1 and resetting with R = 0 and S = 1 (its inputs are "active low").

🔬 Lab: SR flip-flop

Choose the gate type, then change S and R. The log shows each new stable state. Try to break it.

S R
0
Q
1
Q′
Task 19.02 Find a diagram for a NAND gate SR flip-flop and construct the truth table.
Show answer

The NAND version is drawn in exam question 4 (Summary & Exam tab): S feeds the top NAND gate (output Q), R feeds the bottom one (output Q′), and each output feeds back to the other gate. Q = NAND(S, Q′) and Q′ = NAND(R, Q).

SRQQ′Meaning
11QQ′no change (memory)
0110set
1001reset
0011invalid

The invalid input is now S = R = 0, because a NAND gate outputs 1 whenever any input is 0.

⏱️ The JK flip-flop

Besides the invalid state, a circuit can end up in an uncertain state if the inputs do not arrive at quite the same time. To prevent this, a circuit can have a clock pulse input, which gives a better chance of synchronising the inputs. The JK flip-flop is an example.

Figure 19.06(a) A symbol for a JK flip-flop and (b) a possible circuit
Tip There is one special case for a NAND gate: when all inputs are 1 the output is 0. There is one special case for a NOR gate: when all inputs are 0 the output is 1.

Remember that a NAND gate gives a 1 unless all its inputs are 1. In the unset state, Q = 0 and Q′ = 1, the circuit is stable:

  • the clock and K both input 0 (the book prints "R" here; in a JK flip-flop it is K);
  • so the top-left NAND gate outputs 1;
  • which gives two 1 inputs to the top-right NAND gate;
  • ensuring Q = 0.

If J now becomes 1 and the clock pulse switches to 1:

  • the top-left NAND gate has all its inputs at 1, so it outputs 0;
  • this makes the top-right NAND gate output 1 for Q;
  • now the bottom-right NAND gate has two 1 inputs, so Q′ becomes 0.

So J is a set input, and by a similar argument K is a clear input. Like the SR flip-flop it stores one bit, with one important difference: if J and K are both 1, Q and Q′ are toggled (they swap values). No combination of inputs leaves the stored value uncertain, so the JK flip-flop is more reliable.

JKClockQ
00↑Q unchanged
10↑1
01↑0
11↑Q toggles

Table 19.04 Part of the truth table for a JK flip-flop

🔬 Lab: clock a JK flip-flop

Set J and K, then send a clock pulse. Nothing changes between pulses, whatever J and K do. The timing diagram records every pulse.

J K
0
Q
1
Q′
Question 19.02 Can you follow the logic in the JK flip-flop circuit to see that the truth table is an accurate representation of the logic for all combinations of input?
Show answer

While the clock is 0, both left-hand NAND gates output 1, so the right-hand pair just holds its state (unchanged, like S = R = 0). When the clock goes to 1:

  • J = 0, K = 0: each left gate has a 0 input, still outputs 1: Q unchanged.
  • J = 1, K = 0: the top-left gate gets J, clock and the fed-back Q′. If Q′ = 1 it outputs 0 and sets Q = 1. If Q was already 1 then Q′ = 0, it outputs 1 and nothing changes: Q = 1.
  • J = 0, K = 1: the mirror image: the bottom-left gate clears it: Q = 0.
  • J = 1, K = 1: only the gate whose fed-back output is 1 can fire. If Q = 1, the bottom gate (fed by Q) fires and clears it; if Q = 0, the top gate (fed by Q′) fires and sets it: Q toggles.

🧮 19.03 Boolean algebra basics

Chapter 4 (Section 4.01) introduced logic expressions: propositions joined by Boolean operators. Boolean algebra is a simpler way to write them, plus a set of rules for manipulating them.

With any algebra you must be clear what the symbols mean. Here are four meanings of 1 + 1:

StatementApplies to
1 + 1 = 2denary arithmetic
1 + 1 = 10binary arithmetic
1 + 1 = 0bit arithmetic (the sum bit, carry dropped)
1 + 1 = 1Boolean algebra

In Boolean algebra 1 means TRUE, 0 means FALSE and + means OR. So the fourth line reads TRUE OR TRUE is TRUE.

Notations vary: AND and OR are sometimes written ∧ and ∨, and A AND B can be written A.B or AB. This book uses the dot. NOT A, the inverse of A, is written A.

Tip You do not need to remember the ∧ and ∨ notation.

📜 The laws (identities) Table 19.05

Identity / LawAND formOR form
Identity1.A = A0 + A = A
Null0.A = 01 + A = 1
IdempotentA.A = AA + A = A
InverseA.A = 0A + A = 1
CommutativeA.B = B.AA + B = B + A
Associative(A.B).C = A.(B.C)(A + B) + C = A + (B + C)
DistributiveA + B.C = (A + B).(A + C)A.(B + C) = A.B + A.C
AbsorptionA.(A + B) = AA + A.B = A
De Morgan'sA.B = A + BA + B = A.B
Double ComplementA = A

Don't worry if some of the names are unfamiliar. Notice that every identity except one has an AND form and an OR form, and you can turn one into the other by swapping each 0 with 1 and each AND with OR. For example 0.A = 0 (FALSE AND A is FALSE) becomes 1 + A = 1 (TRUE OR A is TRUE).

Some identities look like ordinary algebra, with AND acting like multiplication and OR like addition. That is why we talk about products and sums in Boolean algebra.

De Morgan's laws look complicated but are easy to rationalise: the inverse of a product becomes the sum of the inverses, and the inverse of a sum becomes the product of the inverses ("break the bar, change the sign").

Task 19.03 It is vital that you can interpret a Boolean expression correctly. Go through Table 19.05 item by item and read out the full meaning. For example, 1 + A = 1 can be read as "one plus A equals 1" but must be understood as "TRUE OR A is TRUE".

🗣️ Read it out loud

Say the meaning in words, then reveal it.

🎮 Name that law

Which law justifies each step?

🧩 Worked Example 19.01

Using the algebra to simplify an expression cannot be learned as a routine. It needs some lateral thinking.

WORKED EXAMPLE 19.01

Using Boolean algebra to simplify an expression: show that A + A.B can be simplified to A + B.

  1. First make it more complicated! The OR form of absorption says A + A.B = A. Use it in reverse to replace A by A + A.B: A + A.B + A.B
  2. Ignore the first A for the moment. The AND form of the commutative law and the OR form of the distributive law (in reverse) give: A.B + A.B = B.A + B.A = B.(A + A)
  3. The OR form of the inverse law turns A + A into 1. Bringing back the A: A + B.1
  4. The AND form of the identity law turns B.1 into B: A + B ✔

⚖️ Equivalence checker

Simplified something? Check it here: the checker builds a truth table for both expressions. Use letters for inputs, . (or nothing) for AND, + for OR, and ' after a letter or bracket (or ! before it) for NOT. Example: A + A'.B.

🧾 19.04 Creating an expression directly from a truth table

One formal approach is the sum-of-products method. You write a minterm for each row of the truth table whose output is 1, then OR the minterms together.

For the half adder (Table 19.01), only one row gives C = 1: A = 1 and B = 1. The product is A.B, and the sum of products has just this one term:

C = A.B

Two rows give S = 1, so its sum of products has two minterms:

S = A.B + A.B

A 0 in a row is written as the inverse of that input.

Sometimes the laws can be applied straight to parts of the table. Suppose a truth table contains these two rows:

ABCX
0101
0111

X is 1 whether C is 0 or 1, so there must be a minterm with no reference to C: A.B.

🛠️ Minterm builder

Tap the X column to set the outputs. The sum of products is written for you, followed by the simplest form (found the K-map way).

Sum of products:

Simplest form:

🎯 Practice: write the sum of products

Type a Boolean expression that matches the table (any correct form is accepted; then try for the simplest).

🔌 The Boolean algebra representation of a logic circuit

The same ideas can turn a circuit diagram into an expression: use the truth tables of the individual gates, then simplify with the algebra.

WORKED EXAMPLE 19.02

Creating a Boolean algebra logic expression for a half adder circuit

Figure 19.07A half adder circuit (used in Worked Example 19.02)
  1. A and B go into a NAND gate with output W. The first three rows of the NAND truth table give 1, so the sum of products has three minterms: W = A.B + A.B + A.B
  2. W and A go into a NAND gate to give X. Treat NAND as AND followed by NOT. The AND gives the product of the inputs: A.(A.B + A.B + A.B)
  3. The distributive and inverse laws simplify this: 0 + 0 + A.A.B, which is simply A.B
  4. Now apply the NOT, using the AND form of De Morgan's law, which turns A.B into A + B: X = A + B
  5. The same laws applied to the other intermediate NAND gate give: Y = A + B
  6. Finally A + B and A + B go into the last NAND gate. The AND part gives the product: (A + B).(A + B)
  7. Instead of multiplying it out, apply De Morgan's law directly to perform the inverse: S = A.B + A.B ✔This is exactly the expression we got from the truth table, so the algebra has been used correctly.
Extension Question 19.01 Worked Example 19.02 did not show that the circuit produces the correct output for C. Also a shortcut was used to reach the final form of S. Can you use Boolean algebra to find the form of C from the circuit, and can you convert the expression for S if you start by using the distributive law before applying De Morgan's law?
Show answer

C: both inputs of the last NAND gate are W, so C = W.W = W (idempotent). W = A.B, so C = A.B = A.B (double complement).

S, the long way: multiply out the product with the distributive law:

(A + B).(A + B) = A.A + A.B + B.A + B.B = A.B + A.B

(A.A and B.B are 0 by the inverse law.) Now invert it with De Morgan's law twice:

S = A.B + A.B = (A + B).(A + B) = A.A + A.B + B.A + B.B = A.B + A.B

🗺️ 19.05 Karnaugh maps (K-maps)

A Karnaugh map is a way of creating a Boolean expression from a truth table. It is usually much easier than writing minterms with sum of products and, used correctly, it gives the simplest possible expression.

ABX
000
011
101
111

Table 19.06 The truth table for the OR operand

Sum of products gives:

X = A.B + A.B + A.B

That is not obviously A + B, although the algebra could show it. The K-map is simpler. Each cell holds the output X for one combination of A and B:

Figure 19.08A K-map of the truth table in Table 19.06 (OR), with a row group and a column group

The rules for reading a K-map

  • Only cells containing a 1 are considered.
  • Groups of 1s are identified where possible; a group is a row, a column or a rectangle.
  • Groups must contain 2, 4, 8 and so on cells.
  • Each group should be as large as possible.
  • Groups can overlap, and all overlapping groups must be used.
  • If a cell cannot be put in any group, it is treated as a group on its own.
  • Within each group, keep only the inputs whose value stays constant throughout the group.

In Figure 19.08 there is a column group and a row group. In the column, B stays the same but A changes, so B is kept. In the row, A stays the same. The expression is the sum of what is kept:

X = A + B

The K-map found the OR expression without any algebra.

🧩 Worked Example 19.03

WORKED EXAMPLE 19.03

Using a K-map to interpret a three-input problem

ABCX
0001
0010
0101
0111
1000
1010
1101
1111

Table 19.07 A sample truth table with three inputs

Before using any method, look for trends. Whenever B = 1, X is 1, so the answer should be "B + something".

Sum of products gives five minterms:

A.B.C + A.B.C + A.B.C + A.B.C + A.B.C

There are two ways to lay out the K-map. Here the input values are combined in the columns: rows for A, columns for B and C.

Figure 19.09A K-map representation of the truth table shown in Table 19.07: a square group and a wrap-round row group

The column labels do not follow binary order. They follow the Gray code sequence (00, 01, 11, 10), where only one bit changes each time.

The first group is the square of four 1s (blue rectangle). In it A changes and C changes, but B is constant, so only B is kept, as predicted.

That seems to leave the top-left cell, BC = 00, isolated. It is not, because K-maps wrap round. BC = 01 is next to it but holds a 0. Its other neighbour is BC = 10, at the far end of the row. So there is a row group of BC = 00 and BC = 10 (the dotted partial outlines). In it A stays the same, B changes and C stays the same, so the product is A.C. Adding the B from the other group:

A.C + B

That is much simpler than the five-minterm expression from the truth table.

🔬 K-map lab

Tap cells to set them to 1 or 0. The lab finds the largest groups (coloured dots show which groups each cell belongs to) and writes the simplest sum of products. The small number in each cell is its row number in the truth table.

Simplest expression:

🎯 Practice: simplify with a K-map

Draw the K-map on paper for this table, then type your simplified expression.

🧠 Extension Question 19.02

Extension Question 19.02 Consider the Karnaugh map shown in Figure 19.10. This corresponds to a problem with four inputs. It wraps round horizontally and vertically. Use the map to create a Boolean algebra expression.
Figure 19.10A K-map for a four-input problem (Extension Question 19.02)
Show answer
  • The 2 × 2 square in rows AB = 00, 01 and columns CD = 11, 10: A = 0 and C = 1 are constant: A.C.
  • The overlapping 2 × 2 square in rows AB = 01, 11 and columns CD = 11, 10: B = 1 and C = 1: B.C.
  • The top-left and bottom-left cells (AB = 00 and AB = 10, CD = 00) are a pair because the map wraps vertically: B = 0, C = 0, D = 0: B.C.D.
X = A.C + B.C + B.C.D

Load "Figure 19.10" into the K-map lab to see the groups.

Reflection Point: When using Karnaugh maps, will you remember to use Gray coding if there are three inputs, and will you remember to use all overlapping groups?

📌 Summary

  • Binary addition can be carried out using a half adder or a full adder circuit.
  • SR or JK flip-flop circuits can be used to store a bit value.
  • There are Boolean algebra laws that can be used to simplify logic expressions.
  • The sum-of-products method can be used to create an equivalent logic expression containing minterms from a truth table.
  • A Karnaugh map is a representation of a truth table that allows a simplified logic expression to be derived from it.

🃏 Key-term flashcards

Tap a card to flip it.

✅ Quick quiz

📝 Exam-style questions

Write your answer first, then open the model answer. These are my mark-scheme-style points, not official mark schemes.

Question 1Exam question 1: a circuit with inputs A and B and outputs S and R

(i) OR, AND, NOT.

(ii) The top path is A + B; the AND gate gives A.B (which is also R); the NOT gives A.B; the final AND gives S.

ABA + BA.BA.BSR
0000100
0110110
1010110
1111001
  • It is a half adder.
  • S is the sum bit (S = (A + B).A.B, which is XOR).
  • R is the carry bit (R = A.B).

(i) X = A.B + A.B + A.B (one mark per minterm).

(ii) Whenever A = 1, X = 1 whatever B is, so the simplified expression will contain a term that is just A (A + …). The remaining 1 (A = 0, B = 0) is covered by B, giving X = A + B.

Question 2bExam question 2b: an AND gate, an OR gate and a NOT gate feeding a final AND gate

(i) A.B (the AND truth table is 1 only when both inputs are 1).

(ii) A + B. By sum of products: A.B + A.B + A.B, which simplifies to A + B.

(iii) A + B = A.B (OR form of De Morgan's law).

Teacher's note: the final gate gives X = A.B.A.B = 0, so this circuit always outputs 0. The question only asks about the intermediate outputs.

BB
A10
A11

(ii) Row group A = 1 gives A; column group B = 0 gives B. So X = A + B.

(i) X = A.B + A.B + A.B

(ii)

= A.B + A.B + A.B + A.B  (idempotent, the hint) = B.(A + A) + A.(B + B)  (distributive) = B.1 + A.1  (inverse) = A + B  (identity, commutative)
ABX
001
011
101
110
Question 4Exam question 4: an SR flip-flop constructed using two NAND gates

(i) Q = NAND(S, Q), Q = NAND(R, Q):

SRQQ
Initially1001
R changed to 11101
S changed to 00110
S changed to 11110
S and R changed to 00011

(ii) S = 0 and R = 0. A NAND gate outputs 1 if any input is 0, so both Q and Q become 1. That breaks the rule that Q is the complement of Q, and when S and R return to 1 the final state depends on which input changes first, so it is unpredictable.

(c)(i) A clock (pulse) input.

(c)(ii) Any one: there is no invalid input combination (J = K = 1 toggles the output); the clock synchronises the inputs, so the circuit cannot enter an uncertain state.

(d) Each flip-flop stores one bit. Groups of flip-flops are used to build registers, static RAM (cache) and counters/shift registers.

Cambridge International AS & A Level Computer Science 9608, Paper 31 Q5, June 2016

Question 5Exam question 5: two half adders and an OR gate, with inputs P, Q, R and outputs J, K

In this half adder, output A is the AND (carry) and B is the XOR (sum). The first half adder gives P.Q and P⊕Q; the second gives (P⊕Q).R and P⊕Q⊕R; J ORs the two carries.

(i)

PQRP.QP⊕Q(P⊕Q).RJK
00000000
00100001
01001001
01101110
10001001
10101110
11010010
11110011

(ii) A full adder.

(iii) J = carry out (Cout) and K = sum (S). K is 1 when an odd number of P, Q, R are 1: the sum bit of adding three bits. J is 1 when two or more of the inputs are 1: the carry to the next column. R acts as the carry in from the previous column.

Question 5bExam question 5b: a circuit with inputs A, B and C and output X

(i) X = A.((A + B).C)

(ii)

X = A.(A + B).C  (associative) = (A.A + A.B).C  (distributive) = (A + A.B).C  (idempotent) = A.C  (absorption)

Or in one step: A.(A + B) = A by the AND form of absorption, so X = A.C.

Cambridge International AS & A Level Computer Science 9608, Paper 32 Q5, November 2016

Guru Jazzy · Cambridge AS & A Level Computer Science · Chapter 19 Logic circuits and Boolean algebra · Figures from the Cambridge coursebook (Langfield & Duddell)