Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Working out a circuit's output (logic gates)

After this lesson: I can work out a logic circuit's output.

50 min lesson3 commandsCompilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

When does the cash drawer open?

At a shop till, the cash drawer opens only when a sale is finished and the cashier presses the drawer key. A manager's card opens it at any time.

A rule like that is built from tiny parts that each take a yes or a no. The processor in the till is made of millions of them.

By the end of this lesson

Note

I can work out a logic circuit's output.

Key words

WordWhat it meansExample
Logic gatea tiny part that turns input bits into one output bit
Inputa bit going into a gate
Outputthe bit a gate gives back
Truth tablea table of every input and its output
Boolean expressiona circuit written in words such as AND, OR and NOTA AND B
Half addera circuit that adds two bits

The cells students fill in are left blank in this preview.

Three basic gates

A 1 means yes, or on. A 0 means no, or off.

An AND gate gives 1 only when both inputs are 1. An OR gate gives 1 when at least one input is 1. A NOT gate has one input and flips it. A NAND gate is AND followed by NOT. A NOR gate is OR followed by NOT.

Truth table for AND and OR

ABA AND BA OR B
0000
0101
1001
1111

The cells students fill in are left blank in this preview.

Before you start

Note

Press Start the lab on the bar at the top and wait for the machine to say it is running. Each Run button types its command into the machine's terminal, one after another.

Python and the shell have gates

In Python and in the shell, & is AND, | is OR and ^ is XOR. XOR gives 1 when the two inputs are different.

Given two whole numbers, each one applies its gate to every pair of bits, place by place.

AND on every pair of bits

Command
python3 -c "print(1 & 1, 1 & 0, 0 & 1, 0 & 0)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Three gates on 12 and 10

Command
echo $(( 12 & 10 )) $(( 12 | 10 )) $(( 12 ^ 10 ))

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Worked example: gates on 12 and 10

BitsDenary
12110012
10101010
12 AND 1010008
12 OR 10111014
12 XOR 1001106

The cells students fill in are left blank in this preview.

Reading a circuit as an expression

Gates can be joined, so the output of one is the input of the next. We write the circuit as a Boolean expression: Q = (A AND B) OR (NOT C).

Work it out like a sum. Do the brackets first, then the gate that joins them. Three inputs give eight rows in the truth table.

Worked example: its truth table

ABCA AND BNOT CQ
000011
001000
010011
011000
100011
101000
110111
111101

The cells students fill in are left blank in this preview.

Two gates add two bits

In binary, 1 + 1 is 10: write 0 and carry 1. A half adder does this with two gates.

The sum bit is A XOR B. The carry bit is A AND B. Chains of adders like this one are how a processor adds whole numbers.

The sum and carry of 1 + 1

Command
python3 -c "print(1 ^ 1, 1 & 1)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think OR means one or the other, but not both. In logic, OR gives 1 when both inputs are 1 as well. The gate for "one but not both" is XOR.

Going further: adding three bits

A half adder adds two bits. To add long numbers, each place also has to add the carry from the place on its right. That needs a full adder, made from two half adders and an OR gate.

Work out by hand the sum bit and the carry bit of 1 + 1 + 1. Then of 1 + 0 + 1.

About this lab and its source: Compilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.