Skip to main content
This tutorial builds a four-bit ripple-carry adder from the standard FullAdder, using only the public API. The result is 60 NAND gates, organized as a hierarchy you can inspect.
1

Define the ports

Two four-bit operands (a0–a3 and b0–b3, least significant bit first), a carry in, four sum bits and a carry out.
2

Chain the full adders

Each stage’s carry out feeds the next stage’s carry in. A SourcePort variable tracks the carry as the loop runs.
The template literal keys like inputs[`a${i}`] are still type-checked, because i is the literal union 0 | 1 | 2 | 3.
3

Add some numbers

The settle time depends on how far the carry has to ripple from the previous input.
4

Test every combination

With 4 + 4 + 1 input bits there are only 512 cases, so you can check them all:
The library’s own test suite runs exactly this check.
5

Look inside

elaborate() flattens the hierarchy into primitive gates:
Each full adder is two half adders (an XOR of 4 NANDs plus an AND of 2) and an OR of 3 NANDs, which comes to 15 NANDs per bit.