We could break the above discrete four-bit adder diagram down even further, showing how each gate can be made from NAND gates.īut, like in programming, the final stages of optimization are subtle, repetitive, and better left to automated tools. (Remember: we wrote all of the other gates in terms of NAND!)
#Nand x programmer full
The four-bit adder is made of four full adders stacked vertically here due to screen width limitations, but they'd be a bit easier to read if they were arranged horizontally.Īll of this was built from our original NAND implementation. (The second full adder is highlighted with a box.)
#Nand x programmer plus
(The first half adder is highlighted with a box.)įull adders are always two half adders plus an OR, so a total of two XORs, two ANDs, and one OR. Half adders are always one XOR and one AND, with the XOR placed above the AND. Instead of tracing each connection, here are a few salient points to note.
#Nand x programmer code
Ultimately, our high-level code gets compiled down into native code that would be difficult to read, just as the full diagram below is more difficult than the one above. We write high level code broken into self-contained functions and modules, like the the full adders in the diagram above act as "black boxes" with inputs and outputs. Instead, compare it to the more familiar process of programming. It's not important to read and understand this diagram in detail by tracing each connection. We could simplify it in several ways, but some redundancy is left in to show the repetitive structures that we've built up incrementally by reusing gates, half adders, and full adders. The diagram below shows the full four-bit adder with all of the individual AND, OR, and XOR gates shown. Still, ripple adders are both the historical and conceptual basis for modern adders. Modern CPUs contain several adders doing different jobs, each specialized for its task, all of which are more complex than this basic design. Later, in more advanced processors like Intel's 8008 CPU, faster carry-lookahead designs took over. This is roughly how integers worked in many early microprocessors. Our 4-bit adder is a specific type called a ripple adder because the carry bit "ripples" through the four full adder stages, one by one. The first carry_in and last carry_out are intentionally unused. If we built a five-bit adder, that final carry would be an input to the fifth full adder, and we'd get, which is 16 in base 10.Īs usual, here's the four-bit adder as a circuit diagram made out of our full adder components. The adder worked correctly here: it set the final carry variable to 1, but that value was discarded because our function didn't do anything with it. The problem with fixed-size adders like this one (and the ones in all of our computers) is that they stop being "adders" in the everyday sense when numbers get too large.įor example, if we try to add 15 + 1, which should give us 16, we get 0 instead. # Check: 7 + 8 = 15 (the largest 4-bit number) This is still the same process from primary school: add each column of digits, possibly carrying a 1 over to the next column.
The full adders will cascade into each other: the carry output of each full adder feeds the carry input of the next.
To add multi-bit numbers, we need one full adder per bit. We could use Python's native integers, but keeping the bits explicit makes it clear that there's no magic involved.
First, a note about representation: we'll represent multi-bit numbers with arrays of 0s and 1s.