Lesson 2. The full adder and 4 bits: the carry runs through the digits
In picturesAfter this lesson a display will show the sum of two 4-bit numbers — from 0 + 0 to 15 + 15 = 30. We build the full adder of one digit FA and put four such digits in a chain — that is ADDER4.
2.1. The idea: column addition
Binary numbers are added the same way as decimal ones at school — in columns, right to left. Each column adds three digits: the two digits of the numbers and the carry from the column on the right. The result is a digit of the sum and a carry onwards:
A,B— the digits of the numbers,CI(carry in) — the carry that came from the right;S— the sum digit of this column,CO(carry out) — the carry into the next column.
Three bits are added by two half adders: the first does A + B, the second adds CI to their sum. A carry onwards is needed if at least one of them gave one: CO = c1 OR c2. Both cannot give a carry at once — the sum of three bits is at most 11 (three).
Four FA in a row are four columns. The CO of each digit goes to the CI of the next, so the carry runs from right to left through all the digits, like a wave. The top carry CO is the fifth digit of the sum: 15 + 15 = 30 does not fit in 4 bits.
2.2. Building
The same project as lesson 1. Delete the lesson 1 setup (switches A, B, the HA chip on the canvas, lamps); keep Power/Ground with the labels VCC/GND. The HA and OR chips stay in the library. Everything on labels (wires are fine too).
If you have FA from the "A Watch and a Game from Gates" kit — use it, the pins are the same: A, B, CI → S, CO.
Step 1. FA — the full adder
Adds three bits: A, B and the carry from the lower digit CI. First A + B with a half adder, then the carry is added to the sum with the second half adder; the carry out CO is set if either of them gave a carry.
| Part | Pin | Label |
|---|---|---|
| Input port | out | A |
| Input port | out | B |
| Input port | out | CI |
| HA (1st) | A B S C | A B s1 c1 |
| HA (2nd) | A B S C | s1 CI S c2 |
| OR | I0 I1 O0 | c1 c2 CO |
| Output port | in | S |
| Output port | in | CO |
Ctrl+G → name FA. Chip pins: inputs A, B, CI, outputs S, CO.
Step 2. ADDER4 — four digits
The carry runs from the lowest digit to the highest: the CO of each FA goes to the CI of the next.
| Part | Pin | Label |
|---|---|---|
| Input port | out | A0 |
| Input port | out | A1 |
| Input port | out | A2 |
| Input port | out | A3 |
| Input port | out | B0 |
| Input port | out | B1 |
| Input port | out | B2 |
| Input port | out | B3 |
| Input port | out | CI |
| FA (digit 0) | A B CI S CO | A0 B0 CI S0 c1 |
| FA (digit 1) | A B CI S CO | A1 B1 c1 S1 c2 |
| FA (digit 2) | A B CI S CO | A2 B2 c2 S2 c3 |
| FA (digit 3) | A B CI S CO | A3 B3 c3 S3 CO |
| Output port | in | S0 |
| Output port | in | S1 |
| Output port | in | S2 |
| Output port | in | S3 |
| Output port | in | CO |
Ctrl+G → name ADDER4. Pins: inputs A0–A3, B0–B3, CI; outputs S0–S3, CO.
Step 3. Canvas: switches, adder, displays
| Part | Pin | Label |
|---|---|---|
| Input | out | A0 |
| Input | out | A1 |
| Input | out | A2 |
| Input | out | A3 |
| Input | out | B0 |
| Input | out | B1 |
| Input | out | B2 |
| Input | out | B3 |
| ADDER4 | A0 A1 A2 A3 B0 B1 B2 B3 CI S0 S1 S2 S3 CO | A0 A1 A2 A3 B0 B1 B2 B3 GND S0 S1 S2 S3 S4 |
| Display | ⚙ | width 4, DEC, bus A |
| Display | ⚙ | width 4, DEC, bus B |
| Display | ⚙ | width 8, DEC, bus S |
On the canvas the carry CO is the label S4: it is the fifth digit of the sum, so display S shows numbers up to 30. CI is on GND for now (the carry into the lowest digit = 0) — in lesson 3 it becomes the subtract switch.
2.3. Check
| A | Switches A3…A0 | B | Switches B3…B0 | Display S | |
|---|---|---|---|---|---|
| 3 | 0011 | 4 | 0100 | 7 | |
| 7 | 0111 | 9 | 1001 | 16 | carry: S4 is lit |
| 15 | 1111 | 15 | 1111 | 30 | maximum |
| 0 | 0000 | 0 | 0000 | 0 |
In the "Switches" columns the top switch (A3) is on the left and A0 on the right; 1 — on, 0 — off.
If only the high digits of the sum are wrong, check that the carry goes c1 → c2 → c3 and that the CO of the last FA is on the chip's CO.
Lesson 2 paste (Cmd+I)
Needs HA and OR from lesson 1.
# Lesson 2 — the full adder FA and the 4-bit adder ADDER4.chip FA { portIn(A) portIn(B) portIn(CI) portOut(S) portOut(CO) HA(A, B, s1, c1) HA(s1, CI, S, c2) OR(c1, c2, CO)} chip ADDER4 { portIn(A0) portIn(A1) portIn(A2) portIn(A3) portIn(B0) portIn(B1) portIn(B2) portIn(B3) portIn(CI) portOut(S0) portOut(S1) portOut(S2) portOut(S3) portOut(CO) FA(A0, B0, CI, S0, c1) FA(A1, B1, c1, S1, c2) FA(A2, B2, c2, S2, c3) FA(A3, B3, c3, S3, CO)} input(A0)input(A1)input(A2)input(A3)input(B0)input(B1)input(B2)input(B3)ADDER4(A0=A0, A1=A1, A2=A2, A3=A3, B0=B0, B1=B1, B2=B2, B3=B3, CI=GND, S0=S0, S1=S1, S2=S2, S3=S3, CO=S4)¿No te sale? Algo no funciona →