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
| Word | What it means | Example |
|---|---|---|
| Logic gate | a tiny part that turns input bits into one output bit | |
| Input | a bit going into a gate | |
| Output | the bit a gate gives back | |
| Truth table | a table of every input and its output | |
| Boolean expression | a circuit written in words such as AND, OR and NOT | A AND B |
| Half adder | a 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
| A | B | A AND B | A OR B |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 |
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
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
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
| Bits | Denary | |
|---|---|---|
| 12 | 1100 | 12 |
| 10 | 1010 | 10 |
| 12 AND 10 | 1000 | 8 |
| 12 OR 10 | 1110 | 14 |
| 12 XOR 10 | 0110 | 6 |
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
| A | B | C | A AND B | NOT C | Q |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 | 1 |
| 0 | 0 | 1 | 0 | 0 | 0 |
| 0 | 1 | 0 | 0 | 1 | 1 |
| 0 | 1 | 1 | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 | 1 | 1 |
| 1 | 0 | 1 | 0 | 0 | 0 |
| 1 | 1 | 0 | 1 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 | 1 |
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
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.