LogicGates.org Open the simulatorSimulator
Roadmap

Build a four-bit calculator

Lesson 1 of 1 in this stage, about 45 minutes

A guided build in the simulator: adder chips, subtraction, a digit on the display, and where to go next.

What you are building

A calculator that adds and subtracts two four bit numbers and shows the answer on a seven-segment digit. Nothing in it is new: it is the half adder, the full adder, the ripple carry adder, two's complement and the display decoder from stage 5, wired together. The two widgets below are the heart of it. Everything you build is a way of making the simulator do what they do.

The arithmetic: a four bit ripple carry adder.

Row out 8421
carries 00110
a
+ b
= sum 0 1100

9 + 3 = 12. The sum bits read 1100 = 12, and the carry out is 0, so the answer fits.

The output: a seven-segment decoder and display.

Input 0010 = 2. Lit: a, b, d, e, g, which draws the digit 2.

a b c d e f g

Work through the steps in order, and do not skip a checkpoint. Each step turns the last one into a chip, so a mistake left in the half adder shows up in every adder built from it, where it is much harder to find. Building in the simulator: gates are in the Logic menu, toggles and displays in the Input and Output menus, and File, then Create node, packages whatever you have selected into a reusable chip with the inputs and outputs you named.

Step 1: the half adder chip

  1. Place two toggles, an XOR gate, an AND gate and two displays.
  2. Wire both toggles into both gates. The XOR output is the sum; the AND output is the carry.
  3. Click through the four input combinations.

Checkpoint. 0 + 0 gives sum 0, carry 0. 0 + 1 and 1 + 0 give sum 1, carry 0. 1 + 1 gives sum 0, carry 1.

When all four are right, select everything and use File, then Create node. Name the chip "half adder", its inputs A and B, and its outputs S and C. Delete the loose gates; from now on you place the chip.

Step 2: the full adder chip

  1. Place two half adder chips, an OR gate, three toggles (A, B and the carry in) and two displays.
  2. Wire A and B into the first chip. Wire its S output and the carry in toggle into the second chip.
  3. The second chip's S is the sum. Wire both chips' C outputs into the OR; the OR output is the carry out.

Checkpoint. 1 + 1 + 1 gives sum 1, carry out 1. 1 + 0 + 1 gives sum 0, carry out 1. 0 + 1 + 0 gives sum 1, carry out 0.

The rule: the sum is 1 when an odd number of inputs are 1; the carry out is 1 when at least two are. Check all eight rows, then package it as "full adder" with inputs A, B, C and outputs S, C.

Common mistake: swapping the two half adder outputs

The second half adder must take the first one's sum, not its carry. If the sum column comes out right for single 1s but 1 + 1 + 0 gives the wrong answer, that wire is the first thing to check.

Step 3: four in a row

  1. Place four full adder chips side by side, the rightmost one for bit 0.
  2. Wire each chip's carry out into the carry in of the chip to its left. Give the rightmost chip's carry in a toggle, set to 0 for now.
  3. Place eight toggles: four for A (bits 3 to 0) and four for B. Wire bit 0 of each into the rightmost chip, and so on.
  4. Place five displays: the four sums, and the carry out of the leftmost chip.

Checkpoint. Set A = 1011 (11) and B = 0110 (6).

The sum displays should read 0001 with the carry out at 1: that is 16 + 1 = 17. Then try 1111 + 0001: 0000 with carry out 1, and watch the carry ripple through all four chips. Finally 1010 + 0101 should give 1111 with carry out 0, which is 15 and fits. Package the chain as "4 bit adder" with inputs A3 to A0, B3 to B0 and C, and outputs S3 to S0 and C.

Step 4: subtraction with two's complement

To subtract, add the negative. The negative of B is "invert every bit and add 1", and the adder can do both parts: a row of XOR gates inverts B when a control line is 1, and the same control line goes into the adder's carry in to add the 1.

  1. Add one toggle called "subtract".
  2. Place four XOR gates, one per bit of B. Each takes one B toggle and the subtract toggle, and its output goes to the adder's B input for that bit.
  3. Wire the subtract toggle into the adder's carry in as well, replacing the toggle you put there in step 3.

Checkpoint. With subtract at 0 the adder behaves as before. With subtract at 1, set A = 0110 (6) and B = 0011 (3).

The XORs turn B into 1100, and the sums read 0011, which is 3. The carry out is 1; for subtraction you ignore it. Now try 0011 − 0110, 3 − 6: the sums read 1101, which is −3 in two's complement (top bit worth −8: −8 + 4 + 1). And 0111 − 0111 gives 0000.

Why?: why does one toggle do both jobs?

XOR with 0 leaves a bit alone and XOR with 1 flips it, so the subtract line inverts B only when you ask. The carry in supplies the "add 1" that finishes the negation. When subtract is 0, B passes through unchanged and the carry in is 0, so the same circuit adds. That is how a processor's arithmetic unit does it.

Step 5: a digit on the display

Four displays showing bits are hard to read. The seven-segment decoder from stage 5 turns the four sum bits into a digit. The quickest route is the built in example: open the 7 Segment-display example, which has all seven segment circuits wired to four switches, select the decoder part, and package it as a chip with inputs B3 to B0 and outputs a to g. Or build it yourself from the seven expressions on the decoder page, one segment at a time.

  1. Place the decoder chip and seven displays arranged as a figure eight, a on top, g in the middle.
  2. Wire the adder's four sum outputs into the decoder's inputs, bit 3 to B3 and so on.
  3. Wire each decoder output to its bar.

Checkpoint. 4 + 5 should light a, b, c, d, f and g: a 9. 2 + 1 should light a, b, c, d and g: a 3.

Sums of 10 or more, like 7 + 5, are not decimal digits, so the display shows whatever the don't cares left there. A real calculator would need a second digit and a circuit to split the sum into tens and units; that is beyond four bits, and a good stretch goal.

Step 6: compare with the built in example

Open the Calculator example. Inside it you will find the same half adder and full adder chips, a "4 Bit adder" made of four full adders, and "7 segment decoder" chips on the outputs. It adds only, so your build already does something it does not: subtract. Compare how the two of you wired the carry chain, and open the chips to see where they match your own.

Stretch goals

  • Eight bits. Place a second 4 bit adder chip and wire the carry out of the first into the carry in of the second. Sixteen toggles in, eight sums and a carry out. 200 + 100 needs the ninth bit; check that it lights.
  • An overflow lamp. For signed numbers, overflow is two same-sign inputs giving the other sign. Build it as: A3 XNOR B3 (the signs match), AND S3 XOR A3 (the result's sign differs). Try 5 + 3 and watch it light while 6 − 3 leaves it dark.
  • Two digits. Show sums up to 30 as tens and units. You will need a circuit that turns a five bit number into two BCD digits, which is a truth table with 32 rows and eight outputs: a real design problem, and every tool on this site can help with it.

What to remember

  • Big circuits are built from small chips, and every chip is checked before it is used in the next.
  • One full adder per bit, carries chained, is a ripple carry adder.
  • Inverting B with XORs and setting the carry in to 1 turns the adder into a subtractor.
  • A seven-segment decoder on the sum turns bits into something a person can read.

Build it: the four bit calculator example in the simulator.

Check yourself

Get 5 right in a row and the lesson is done. A wrong answer costs the run, not the lesson.

0 right in a row. 0 / 0 this visit

A four bit ripple carry adder adds 1010 (10) and 0001 (1). What are the sum bits and the carry out?

1010 + 0001

Already know this? and come back to the quiz any time.

Go deeper