Circuit Bytes · Episode 52 · Electronics · Logic gates
"Adding numbers with gates"
In binary, 1 + 1 is written 10, so adding two bits needs a sum bit and a carry bit. The carry is an AND gate and the sum is an XOR gate; side by side they make a half adder. Chain adders one per column, passing each carry along, and a chip can add numbers of any size.
1 + 1 in binary
Binary has only two digits, 0 and 1, so there is no digit two. When you add 1 + 1, the answer is written 10 (say it "one-zero"): a 0 in this column and a 1 carried to the next column on the left. It is the same move you make in decimal when 9 + 1 makes 10: the column runs out of digits, writes 0 and carries 1.
Two bits in, two bits out
Because of that carry, adding two single bits A and B needs two outputs:
| A | B | carry | sum | |---|---|---|---| | 0 | 0 | 0 | 0 | | 0 | 1 | 0 | 1 | | 1 | 0 | 0 | 1 | | 1 | 1 | 1 | 0 |
Read the last row as 10: carry 1, sum 0.
The carry is AND
Look at the carry column: it is 1 only when both bits are 1. That is exactly the AND gate from episode 49.
The sum is XOR
The sum column is 1 when exactly one bit is 1, but not when both are. That rule is called exclusive OR, or XOR. Its symbol is the OR symbol with an extra curved line along its back. (XOR can itself be built from AND, OR and NOT, or from four NAND gates, like everything in episode 51.)
The half adder, and chaining adders
Wire an XOR gate and an AND gate side by side, both fed by A and B, and you have a half adder: the XOR gives the sum bit and the AND gives the carry bit.
To add longer numbers you line up one adder per column and pass each column's carry to the next column on the left, just like carrying in pencil-and-paper addition. For example, 0110 + 0011 (6 + 3) gives 1001 (9), with carries hopping right to left along the row. Columns after the first need a full adder, which also takes the carry coming in from the column before; a full adder is two half adders plus an OR gate. A chain like this is called a ripple-carry adder. Real chips add a fixed number of bits at once, such as 64, and use carry-lookahead tricks so they don't have to wait for the carry to ripple across every column.
Try it yourself
On paper, add 0101 + 0110 in binary, column by column from the right. In each column, write the sum bit and the carry bit you pass to the next column, using the half-adder table above (and remember the carry coming in). Then convert both numbers and your answer to decimal to check your work.
Good to know
- In binary,
1 + 1 = 10: a0with a1carried, just as decimal9 + 1 = 10. - A half adder adds two bits: sum =
A XOR B, carry =A AND B. - XOR (exclusive OR) outputs
1when exactly one input is1. - A full adder also takes a carry in; it is two half adders plus an OR gate. Chaining full adders one per bit gives a ripple-carry adder.
- Fast chips use carry-lookahead logic so the carry does not have to ripple through every bit.
Answer to the comment question
In binary, what's 10 + 10? 100. Add column by column from the right. Ones column: 0 + 0 = 0, no carry. Twos column: 1 + 1 = 10, so write 0 and carry 1 into a new column on the left. That gives 100. In decimal it is just two plus two equals four, and 100 is four in binary.
Test your knowledge
Quick quiz: "Adding numbers with gates"
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- All About Circuits textbook, Vol. IV Digital, chapter 9, "A Half-Adder": sum = A XOR B, carry = A AND B
- CircuitBread mirror of Lessons in Electric Circuits Vol. IV, chapter 9, "Combinational Logic Functions": half-adder and full-adder sections
- Nand2Tetris (Nisan and Schocken, The Elements of Computing Systems), "Project 2: Boolean Arithmetic": half adder, full adder, chaining adders into a multi-bit adder
- All About Circuits textbook, Vol. IV Digital, chapter 2, "Binary Addition": binary addition with carries, 1 + 1 = 10
Video transcript
How does a chip work out one plus one?
In binary there's no digit two. One plus one is written one-zero: a zero, and a one carried to the next column, just like nine plus one makes ten.
So adding two bits needs two outputs: a sum bit and a carry bit.
The carry is one only when both bits are one. That's an AND gate.
The sum is one when exactly one bit is one, not both. That's exclusive OR, or XOR.
Wire them side by side and you've built a half adder.
Chain adders, one per column, passing each carry along, and a chip adds numbers of any size.
In binary, what's one-zero plus one-zero? Comment your answer below!
That's your byte.
More from Circuit Bytes
- EP50c"How transistors make a gate"
- EP51c"NAND: the universal gate"
- EP53c"Flip-flops: one bit of memory"
- EP54c"Clock signals"