4 / 6 · Intro
How gates add numbers
One XOR and one AND gate are enough to add two bits. Chain four full adders and you can add any two numbers from 0 to 15.
Jump to the 10 practice questionsYou will learn
- Add binary numbers with carries
- Build a half adder and a full adder from gates
- Spot overflow in a fixed number of bits
Before this: Logic gates
1Explain
Add 0101 and 0011 on paper
Try this before reading on. Add 0101 and 0011 on paper, in binary, the way you add in decimal, and write each carry above the next column. You need only three facts: 0 + 0 = 0, 0 + 1 = 1, and 1 + 1 = 10, which means write 0 and carry 1. With a carry coming in, 1 + 1 + 1 = 11, so you write 1 and carry 1. The working is in the next paragraph, so cover it up first.
2Explore
The working, column by column
Here is the working. Rightmost column: 1 + 1 = 10, write 0, carry 1. Next: 0 + 1 plus the carry is 10 again, write 0, carry 1. Next: 1 + 0 plus the carry, 10, write 0, carry 1. Leftmost: 0 + 0 plus the carry is 1. The answer is 1000, which is 8, and 5 + 3 = 8. Three of the four columns carried. A calculator, a game and a spreadsheet all end up doing exactly this.
Drag to turn. Scroll or pinch to zoom.
3Problem
1 + 1
Hint
Write 0, carry 1.
Solution
Answer: 10
2 is written 10: sum 0, carry 1.
4Explain
The rightmost column needs only two gates
Look at the rightmost column on its own. The answer bit there, the sum, is 1 when exactly one input is 1. That is the XOR rule. The carry is 1 only when both inputs are 1. That is the AND rule. So an XOR gate and an AND gate side by side, fed the same two inputs, add two bits. This little circuit is a half adder. It is only "half" because it has no way to take in a carry from a column to its right.
5Problem
The sum bit
In a half adder, which gate gives the sum bit?
Hint
1 when exactly one input is 1.
Solution
Answer: XOR
The sum is 1 when exactly one input is 1: XOR.
6Problem
The carry bit
In a half adder, which gate gives the carry?
Hint
1 only when both inputs are 1.
Solution
Answer: AND
The carry is 1 only when both inputs are 1: AND.
7Explain
The other columns have three bits to add
In every other column three bits meet: one from each number and the carry from the right. A full adder takes all three. Its sum is A XOR B XOR carry-in, which is 1 when an odd number of the three are 1. Its carry-out is 1 when at least two of them are 1. Two half adders and an OR gate make one full adder. Line up four full adders, wire each carry-out to the carry-in of the adder on its left, and you have a 4-bit adder. Each carry has to ripple from right to left before the last column settles, so this design is called a ripple-carry adder.
| A | B | Carry in | Sum | Carry out |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 | 1 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 |
8Explore
A full adder in code
The first line is the sum bit, the second the carry out. Set all three inputs to 1 and both are 1.
const a = 1, b = 1, cin = 0;
const sum = a ^ b ^ cin;
const cout = (a & b) | (cin & (a ^ b));
console.log(sum);
console.log(cout);0
19Problem
Inputs of a full adder
How many input bits does a full adder take?
Hint
One bit from each number, and the carry.
Solution
Answer: 3
A bit from each number plus the carry from the right.
10Problem
Three 1s
A full adder gets A = 1, B = 1, carry in = 1. What is its sum bit?
Hint
The sum is 1 for an odd count of 1s.
Solution
Answer: 1
1 + 1 + 1 = 3 = 11 in binary: sum 1, carry 1.
11Problem
0110 + 0011
Hint
6 + 3 = 9.
Solution
Answer: 1001
6 + 3 = 9 = 1001.
12Problem
An 8-bit adder
How many full adders does a ripple-carry adder for 8-bit numbers need?
Hint
One full adder per column.
Solution
Answer: 8
In the design used here, each full-adder stage handles one bit position and its incoming carry. For 8-bit numbers, the 8 positions therefore use 8 full adders.
13Explain
What happens when the answer is too big
Four bits hold 0 to 15. Add 9 and 8 and the true answer, 17, is 10001 in binary, which is five bits. A register that keeps only four bits stores 0001, which is 1, and the carry left over on the left is the warning sign. That is overflow. 15 + 1 in four bits leaves 0000. Processors set a flag when it happens so that programs can notice. Subtraction reuses the same adder. To work out A − B, the processor flips every bit of B and adds 1, which gives the two's complement of B, then adds that to A and drops the extra carry.
14Problem
9 + 8 in four bits
A 4-bit register adds 9 + 8 and keeps only four bits. What number does it hold?
Hint
17 is 10001; keep the four lowest bits.
Solution
Answer: 1
17 = 10001; the four lowest bits are 0001 = 1. That is overflow.
15Problem
The largest 4-bit sum
What is the largest sum two 4-bit numbers can make?
Hint
15 + 15.
Solution
Answer: 30
15 + 15 = 30 = 11110, five bits.
16Problem
Two's complement of 0011
Hint
Flip every bit, then add 1.
Solution
Answer: 1101
Flip the bits: 1100. Add 1: 1101.
17Explain
Multiplying is shifting and adding
Multiplication comes from the same parts. In binary, multiplying by 2 moves every bit one place to the left, just as multiplying by 10 adds a zero in decimal: 0011 (3) becomes 0110 (6). For 5 × 3, notice that 3 is 2 + 1. So 5 × 3 = 5 × 2 + 5 × 1 = 1010 + 0101 = 1111, which is 15. A multiplier circuit shifts and adds like this, taking the 1 bits of the second number one at a time.
18Practice
A 4-bit ripple adder
Finish add4(a, b). It walks the columns from right to left; in each column work out the sum bit and the new carry with a full adder. It returns the carry out, a space and the four sum bits.
0 1000
0 1001
1 0001function add4(a, b) {
let carry = 0, out = "";
for (let i = 3; i >= 0; i--) {
const x = Number(a[i]), y = Number(b[i]);
const sum = 0;
out = sum + out;
}
return carry + " " + out;
}
console.log(add4("0101", "0011"));
console.log(add4("0110", "0011"));
console.log(add4("1001", "1000"));
Hint
The carry out is 1 when at least two of x, y and the carry in are 1.
A solution
function add4(a, b) {
let carry = 0, out = "";
for (let i = 3; i >= 0; i--) {
const x = Number(a[i]), y = Number(b[i]);
const sum = x ^ y ^ carry;
carry = (x & y) | (carry & (x ^ y));
out = sum + out;
}
return carry + " " + out;
}
console.log(add4("0101", "0011"));
console.log(add4("0110", "0011"));
console.log(add4("1001", "1000"));19Explain
Common mistakes
"1 + 1 = 2, so the answer fits in one bit."
A bit holds only 0 or 1. 1 + 1 = 10: the sum bit is 0 and a 1 is carried. Drop the carry and the answer becomes 0, which is wrong.
"A half adder can add numbers of any length."
A half adder has no input for a carry from the right, so it only works in the rightmost column. Every other column needs a full adder with three inputs. A 4-bit adder could use one half adder and three full adders.
"Overflow makes the computer stop with an error."
The adder simply keeps the bits it has room for. 9 + 8 in four bits gives 0001, a wrong answer that looks perfectly ordinary. The processor sets a flag, and the program has to check it.
"Subtraction needs its own subtracting circuit."
Flip the bits of B, add 1 and add the result to A with the same adder. 5 − 3: 0101 + 1101 = 10010, and the four lowest bits, 0010, are 2.
20
Summary
- Half adder: sum = A XOR B, carry = A AND B.
- Full adder: three inputs; sum is 1 for an odd count of 1s, carry for two or more.
- Chained full adders form a ripple-carry adder; carries move right to left.
- Overflow: the result needs more bits than the register has. Subtraction adds the two's complement.