← From gates to a processor

Adding with gates

Adding two bits is a truth table: the sum is XOR and the carry is AND, a half adder. A full adder also takes a carry in. One full adder per place, carries passed to the left, adds whole binary numbers.

A processor adds billions of numbers a second, and it is only gates. How do switches add?

Adding in binary works like adding in columns at primary school, with only 0 and 1. Each column is a small truth table, so gates can do it.

PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

1 + 1

In binary, what is 1 + 1?

TryTry it on the board and compare the result with your prediction.

Try: build a half adder

Was your prediction right? Look at the last row of the table.

Try it yourselfYour edits are saved in this browser.

Build the half adder: S (sum) and C (carry) for two bits. Every row should match.

Use the board. This ticks when you get there.

InvestigateWork out why it happens, one step at a time.

Worked example: 5 + 7 in four bits

Worked exampleBefore each step, say what you think comes next.
  1. ones: 1+1=10\text{ones: } 1 + 1 = 10Sum 0, carry 1 to the twos.

Now add 6 + 3.

Add 0110 + 0011 (6 + 3) the same way. What is the sum, in four bits?

Sam adds 0101 and 0011. Click the line where it first goes wrong.

A half adder adds two bits. Why does a full adder need three inputs?

ModifyChange the problem on purpose and see what happens.

Vary: carries

Try it yourselfYour edits are saved in this browser.

Set A = 9 and B = 6. Does any place carry?

Use the board. This ticks when you get there.

Find two numbers whose sum has exactly one carry, and still fits in four bits.

Use the board. This ticks when you get there.

Now make the last carry fall off the end: a sum that needs a fifth bit.

Use the board. This ticks when you get there.

Make every place carry.

Use the board. This ticks when you get there.

PractiseA few on your own, to check you have it.

Adders

Add 0011 + 0011 in binary (four bits).

How many full adders does a processor need to add two 8-bit numbers in one go?

Review: an 8-bit register holds 255. You add 1. What does it hold?

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

Build a full adder

Then: count the gates in an 8-bit adder built from your full adders.

Try it yourselfYour edits are saved in this browser.
Need a hint?

The sum is 1 when an odd number of the three inputs are on.

Another hint

Sum: XOR A with B, then XOR that with CIN.

Another hint

Carry: (A AND B) OR ((A XOR B) AND CIN). Reuse the first XOR.

Build a full adder: inputs A, B and the carry in (CIN); outputs SUM and CARRY. Every row should match.

Use the board. This ticks when you get there.

For teachers

Students build a half adder from XOR and AND, add 4-bit binary numbers on a chain of full adders and watch the carries ripple, then build a full adder from gates (AC9TDI8K04 node 3, extended; the kit names adder circuits as going further). About 45 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

Try the boards yourself.

The one idea: one column of an addition is a small truth table (sum and carry), so gates can add. Students have met the half adder in Data representation's logic gates lesson; here they build it and then go further.

Column addition in base ten first helps: 5 + 5 writes 0 and carries 1, exactly like 1 + 1 in binary.

The 4-bit adder on the board is drawn as full adder boxes; the gates inside each box are the ones students build in the Make.

Running the lesson

  1. Predict: students who chose 2 hold Sam's misconception.
  2. Try: ask which gate makes the carry and why.
  3. Worked example: write the carries above the columns, as in base ten.
  4. Vary: after overflow, ask where the fifth bit would have to go.
  5. Make: let students reuse the first XOR for the carry; compare gate counts.

The hard part (on purpose)

The full adder Make is hard on purpose: it is the first circuit with three inputs and two outputs. Point at the wanted columns and let students build the sum first, then the carry.

When students get stuck

Stretch and support

Stretch: Subtraction: look up two's complement. How could the same adder subtract?

Support: A column-addition frame with a carry row above, for binary.

Where it leads

Next: Remembering a bit (latches and registers).

Links: Data representation › Bits all the way down (overflow); Inside a computer (the processor's speed).

What each step is for

Now you can
Next lesson3. Remembering a bitFeed two gates' outputs back into each other and they hold a bit: a latch. Set makes it 1, Reset makes it 0, and with both off it keeps what it had. Latches side by side make a register that stores a number.