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

Free lesson

Building an adder from logic gates

After this lesson: I can build an adder from logic gates.

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

Switches that cannot count

A shop's till adds a 6 pound sandwich to a 3 pound drink in a few billionths of a second. Yet nothing inside the processor can count.

Each tiny part only answers a yes or no question about two bits. This lesson builds addition out of those questions.

By the end of this lesson

Note

I can build an adder from logic gates.

Key words

WordWhat it means
Logic gatea circuit that turns one or two input bits into one output bit
Truth tablea list of every input and the output it gives
Carrythe bit passed to the next place when a sum is too big
Half addertwo gates that add two bits
Full addergates that add two bits and a carry
Overflowan answer too big for the bits kept

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

Four gates answer questions about bits

A logic gate takes bits in and gives one bit out. AND gives 1 only when both inputs are 1. OR gives 1 when either input is 1, or both. XOR, exclusive or, gives 1 when the inputs differ. NOT has one input and flips it.

A truth table lists every input with its output. Two inputs have only four cases, so the table is short.

Truth table: three gates

ABA AND BA OR BA XOR B
00000
01011
10011
11110

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

Go to the architecture folder

Command
cd ~/arch

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

Apply the gates to 6 and 3

Command
python3 -c "print(6 & 3, 6 | 3, 6 ^ 3)"

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

Worked example: the gates, bit by bit

BitsNumber
A01106
B00113
A AND B00102
A OR B01117
A XOR B01015

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

Two gates add two bits

Adding two bits has four cases. 0 + 0 is 0, and 0 + 1 and 1 + 0 are 1. But 1 + 1 is 2, written 10 in binary.

The right-hand bit of the answer is A XOR B. The left-hand bit, the carry, is A AND B. So one XOR gate and one AND gate make a half adder.

Truth table: a half adder

ABSum (XOR)Carry (AND)
0000
0110
1010
1101

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

XOR is the sum without the carries

Command
python3 -c "print(6 + 3, 6 ^ 3, (6 & 3) << 1)"

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

A full adder takes the carry in

Every place except the first must also add the carry from the place to its right. So it adds three bits.

A full adder does that with two half adders and an OR gate. Chain four of them and you can add 4-bit numbers. The carry ripples from right to left, one full adder to the next, so this design is called a ripple-carry adder.

Worked example: 0110 + 0011, right to left

PlaceABCarry inSumCarry out
1s01010
2s11001
4s10101
8s00110

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

When the answer does not fit

An adder keeps a fixed number of bits. For unsigned numbers, a carry out of the top place is lost, and the answer wraps round.

For signed numbers the test is different. 0110 + 0011 is 9, but in 4-bit two's complement 1001 reads as -7. Two positive numbers gave a negative answer. That is overflow too, although nothing carried out of the top.

Write a program that adds 200 and 100

Command
printf '.globl _start\n_start:\n  li a0, 200\n  li a1, 100\n  add a0, a0, a1\n  li a7, 93\n  ecall\n' > big.s

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

Assemble it

Command
riscv64-linux-gnu-as big.s -o big.o

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

Link it

Command
riscv64-linux-gnu-ld big.o -o big

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

Run it on the emulator

Command
qemu-riscv64 ./big

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

Read the exit status

Command
echo $?

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 overflow always means a carry came out of the top. That is true only for unsigned numbers. In two's complement, 1111 + 0001 carries out, yet -1 + 1 = 0 is right. Signed overflow happens when both inputs have the same sign and the answer has the other sign.

Going further: subtracting with an adder

A processor has no separate circuit for subtraction. It works out a - b as a + (NOT b) + 1, because NOT b + 1 is -b in two's complement.

Write subtract(a, b) with your add: flip every bit of b, add it to a, then add 0001. Try 0101 - 0011 and 0011 - 0101. Then say how hardware could save the second addition.

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.