How computers work

Adder: how a processor adds numbers

Binary addition works like long addition on paper: add the digits from right to left and carry a one into the next digit. A full adder does this for one bit using five gates, and a chain of 64 such circuits adds 64-bit numbers. The same adder lets the processor subtract, compare numbers, and compute memory addresses.

Open in 3D →

Binary numbers and column addition

Inside a computer, numbers are stored in binary: each digit is 0 or 1, and the weight of each digit doubles from right to left: 1, 2, 4, 8, 16… The number 13 is 8 + 4 + 1, that is, 1101. A 64-bit register holds numbers from 0 to 2⁶⁴ − 1 ≈ 1.8·10¹⁹, eighteen quintillion.

Addition works the same way as at school, only simpler: the addition table for a single digit has four rows. 0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1, and 1 + 1 = 10, that is, zero in this digit and a carry of one into the next.

Let’s add 0110 (6) and 0011 (3). Lowest digit: 0 + 1 = 1, no carry. Second: 1 + 1 = 10, write 0, carry 1. Third: 1 + 0 + carry 1 = 10, write 0, carry 1. Fourth: 0 + 0 + 1 = 1. The answer is 1001 = 9. You can see that from the second digit onward, three bits are being added: two from the numbers and the carry.

So a one-bit adder needs three inputs and two outputs: the sum bit, which stays in this digit, and the carry bit, which goes to the neighbor on the left.

The half adder: two bits

The lowest digit adds only two bits, with no incoming carry. The circuit for this is called a half adder. Look at the table: the sum S is 1 when exactly one of the bits is 1, which is XOR. The carry C is 1 only when both bits are 1, which is AND.

So a half adder is two gates: S = A ⊕ B and C = A·B (here ⊕ is exclusive OR, the dot is AND, and the plus in the formulas below is OR).

For every digit except the lowest, a half adder is not enough: the carry from the neighbor must also be taken into account. A full adder can be built from two half adders and one OR, and that is exactly how it is built in the model.

A one-bit full adder

Three bits come in: A and B from the two numbers, and Cin, the carry from the lower digit. Two come out: S (the sum in this digit) and Cout (the carry into the next). The rule: S = A ⊕ B ⊕ Cin, and the carry Cout = A·B + Cin·(A ⊕ B).

In the model this is five gates. The first XOR computes the half sum x1 = A ⊕ B. The second XOR adds the carry: S = x1 ⊕ Cin. The first AND checks whether the bits themselves produce a carry: A·B. The second AND checks whether the half sum together with the incoming carry produces one: x1·Cin. The OR combines both cases into Cout. These two sources of carry never fire at the same time: if A and B are both 1, then x1 = 0.

Let’s go through the row A = 1, B = 1, Cin = 1 (1 + 1 + 1 = 3 = 11 in binary). x1 = 1 ⊕ 1 = 0; S = 0 ⊕ 1 = 1; A·B = 1; x1·Cin = 0; Cout = 1 + 0 = 1. We got S = 1, Cout = 1, that is, 11, which is correct. The level’s demo walks through the eight-row table (all combinations of three bits).

It is worth noting that Cout is the “majority” function: it is 1 when two or three of the three inputs are ones. And S is the parity function: it is 1 when the number of ones is odd.

In the model the full adder is built from textbook gates: two XORs of 16 transistors each, two ANDs and an OR of 6 each, 50 transistors in total. A real optimized full adder (the “mirror adder”) needs 24–28 transistors, and in cells with transmission gates even fewer. Double-clicking a gate opens its transistor schematic.

Inputs, outputs, and the chain of bits

The pins are labeled: A, B, Cin on the left, S and Cout on the right. Wire highlighting shows the signal value: light blue is 1, dark is 0. The values on the wires are taken from the last instruction executed by the teaching core; in the lab you can toggle the inputs by hand.

Change one input bit, and the change ripples through the gates to the outputs. The signal does not arrive instantly: each gate adds its own delay, and the output “settles” only after the change has traveled the longest path. This longest path is called the critical path.

Switch the width to 2, 4, or 8 bits: the adders will be connected into a chain where the Cout of one becomes the Cin of the next. The incoming carry of the lowest bit is usually 0; for subtraction it is 1 (see below).

Carry along the chain

Such a chain is called a ripple-carry adder. It is simple, one full adder per bit, but slow: the highest bit cannot give the correct answer until the carry has run through all the lower ones.

The “carry through all bits” demo adds 0111…1 and 1. In the lowest bit 1 + 1 = 10, and a carry appears; in the second bit 1 + 0 + 1 gives a carry again, and so on up to the highest bit. Try adding 255 + 1 with 8 bits: 11111111 + 00000001 = 1 00000000; the carry runs through all eight bits and falls out the top.

The carry path through one bit is two levels of gates (AND and OR). For 64 bits that is about 128 levels; at a gate delay of 10 ps that is ≈ 1.3 ns, or 6–7 clock cycles of a 5 GHz processor. That is far too slow: in a real core, an addition must fit into one cycle together with selecting the operands and forwarding the result.

In the teaching 64-bit ALU on the Core blocks level, the adder is exactly this kind, with ripple carry, so that the path of every bit is visible. This does not change the logic of the answer, only the time.

Fast carry: generate and propagate

Addition can be sped up thanks to one observation: for each bit, without knowing the incoming carry, you can say in advance one of three things. If A = B = 1, the bit generates a carry itself (generate, g = A·B). If exactly one of the bits is 1, the bit passes the incoming carry on (propagate, p = A ⊕ B). If both are 0, the bit kills the carry. Then Cout = g + p·Cin.

The g and p signals for all bits are computed simultaneously, in one or two levels of gates. Next they are combined in pairs: a group of two bits generates a carry if the higher bit generates it, or if the higher bit propagates and the lower one generates; it propagates if both propagate. Then groups are combined by 4, by 8, by 16… This is a parallel prefix tree.

For 64 bits the tree has log₂64 = 6 levels of combining. The Kogge–Stone adder (1973) computes all 64 carries in these 6 levels, plus a level for g/p and a level for the final XORs: about 8–10 levels of gates instead of 128. The price is area and wiring: a Kogge–Stone tree has hundreds of cells and many long crossing wires. There are compromise designs (Brent–Kung, Han–Carlson, carry-select adders) that sacrifice a level or two to save area.

In the end, a 64-bit addition in a modern core takes on the order of a hundred picoseconds, less than a clock cycle, leaving time for multiplexers and forwarding the result to the next instruction. The formula S = A ⊕ B ⊕ Cin itself does not change: only the computation of the carries is sped up.

Subtraction, negative numbers, and overflow

Negative numbers in a processor are stored in two’s complement: −x is written as (NOT x) + 1. In 4 bits, −1 is 1111, −3 is 1101, and the highest bit works as the sign. The convenience is that such numbers can be added with an ordinary adder without paying attention to the sign: 5 + (−3) = 0101 + 1101 = 1 0010; the extra carry is discarded, leaving 0010 = 2.

That is why there is no separate subtractor: A − B = A + (NOT B) + 1. An inverter on input B and an incoming carry Cin = 1 turn the adder into a subtractor; this is how it is done in the ALU one level up. The “less than” comparison (slt in RISC-V, cmp in x86) is also a subtraction, of which only the sign and the overflow flags are examined.

If the result does not fit into the bit width, this is called overflow, and it differs for unsigned and signed numbers. Unsigned: 1111 + 0001 = 1 0000, a carry out of the highest bit (the C flag), and the 4 bits of the result are 0000. Signed: 0111 + 0001 = 1000, that is, 7 + 1 gave −8. There is no carry out of the highest bit, but the sign of the result is wrong: the V flag. It is computed as the XOR of the carries into and out of the highest bit.

The processor does not decide whether this is an error: it only records the fact in flags (x86, ARM) or reports nothing at all (RISC-V, where the check is done with separate instructions). The program decides what to do: for addresses and hashes, “wrapping around” through zero is normal behavior, while in the C language signed integer overflow is formally undefined behavior, and the compiler is entitled to assume it never happens.

Where the adders are in a processor

The adder is the most heavily used circuit in a core. There is one in every integer ALU (a modern core has 4–6 of them) and in the address generation units (AGUs): a load address is a register plus an offset. Separate adders compute the address of the next instruction, PC + 4, and the branch target address, PC + offset.

A multiplier is also made of adders: multiplying two 64-bit numbers gives 64 partial products (shifted copies of the multiplicand), and they are summed by a tree of adders (Wallace and Dadda trees), which at each level turn three addends into two. That is why multiplication takes 3–4 clock cycles rather than one.

Floating-point units and vector units contain dozens of adders of various widths: for mantissas, exponents, and rounding. A single core has dozens to hundreds of adders in total, and their speed directly limits the clock frequency.

Common misconceptions

“A computer subtracts with a separate circuit.” No, it uses the same adder, with one operand inverted and an incoming carry of 1.

“Adding 64-bit numbers is 64 times slower than adding 1-bit ones.” Only in the simplest chain. With fast carry, the time grows as the logarithm of the bit width: 64 bits take only a few times longer to add than 4.

“Overflow is always an error, and the processor catches it.” The processor only records overflow in flags (or not at all), and 64-bit arithmetic modulo 2⁶⁴ is a normal and widely used mode.

Open in 3D →

All articles

Overviews

Logic: from transistor to arithmetic

Processor

Computer

Servers and data centers

Networks and the internet

Software