Subtraction with the Same Adder
To subtract, the ALU doesn't need a subtractor: it flips every bit of and sets the adder's carry-in to 1, which adds , and is invisible mod . One control bit turns the adder into a subtractor, and a few more choose which result comes out of the ALU.
What the symbols mean
- NOT B
B with every bit flipped; B + NOT B = 2ⁿ − 1.
- Width
The number of bits the adder keeps.
What the symbols mean
- Subtract control
0 to add, 1 to subtract.
- Adder input
What actually reaches the adder's B side.
Explanation
An ALU, short for arithmetic logic unit, is simply the part of the processor that does the math: add, subtract, AND, OR, compare. The surprise in this lesson is that subtraction costs almost nothing extra. The first formula says: to compute , take ( with every bit flipped), add it to , and add 1 more. The "" is the number wheel from the first unit: the adder only keeps bits. Three small pieces, one adder; bear with the why.
Why it works. Add to its flipped self and every column has one 0 and one 1, so there are no carries and you get all 1s: . Move things around: . That's the two's complement of . So
and the adder throws away the , leaving mod . Because unsigned and signed are just two names for the same spots on the wheel, those bits are the right answer under either reading, as long as the answer fits. One subtract instruction serves both.
One adder does both. The trick is an XOR gate on each input. XOR with 0 leaves a bit alone and XOR with 1 flips it (, ), so an XOR is an inverter you can switch on and off. Run one control wire, call it sub, into all XORs and also into the adder's carry-in :
- sub : the adder sees and carry-in 0, and computes ;
- sub : it sees and carry-in 1, and computes .
The never needs its own adder; it rides in on the carry-in, which addition doesn't use. Total extra cost: XOR gates. Negation comes free too: is , so the ALU negates by subtracting from zero.
The carry out means "no borrow". The last formula says the adder's full result is . For unsigned and , that reaches , producing a carry out of the top, exactly when . So after a subtraction, carry out 1 means and carry out 0 means (a borrow happened). The flags lesson builds every comparison on this.
The ALU around the adder. A simple ALU is a stack of identical one-bit slices. Each slice has an optional inverter on each input (controlled by bits Ainvert and Binvert), an AND gate, an OR gate and one full adder, plus a selector that picks one of the three outputs using a 2-bit operation code. Binvert also drives the carry-in of the bottom slice, so it doubles as the sub wire above:
| Ainvert | Binvert | operation | result |
|---|---|---|---|
| 0 | 0 | AND | AND |
| 0 | 0 | OR | OR |
| 0 | 0 | add | |
| 0 | 1 | add | |
| 1 | 1 | AND | AND NOR |
| 1 | 1 | OR | OR NAND |
The last two rows are De Morgan's laws put to work: inverting both inputs of an AND gives a NOR. A handful of control bits, decoded from the instruction, is all that separates add, sub, and, or and nor. There's an animation further down that shows the subtraction trick on the 4-bit wheel.
A common mistake. It's tempting to compute as , since flipping the bits feels like negating. Flipping alone gives the ones' complement, , so you'd get , off by one every single time. The is what turns "flip" into "negate", and in hardware it's simply the carry-in set to 1.
See it
Worked examples
Compute () the way an 8-bit ALU does.
- 1
The ALU sets sub , which flips every bit of : becomes .
Compute in 8 bits and read the result as signed and as unsigned.
- 1
Write the operands: and .
Practice
Type your answer and check it. Solve them all to complete the lesson.
An 8-bit ALU computes . What does the result read as unsigned, and as signed? Enter both.
What is NOT(0x2D) + 1 as an unsigned 8-bit number?
An 8-bit ALU computes . What is the 8-bit result, and what is the carry out? Enter both.
A student's homemade ALU computes and forgets to set the carry-in. What does it output for in 8 bits?
An ALU slice has an AND gate, an OR gate and an adder, with optional inverters on each input controlled by Ainvert and Binvert. Which setting computes NOR(, )?
A robot's wheel encoder counts ticks in an 8-bit unsigned counter that wraps from 255 to 0. The last reading was and the new one is . The controller computes new − old on its 8-bit ALU. What result does it get?
In 8-bit signed arithmetic, the ALU computes . What signed value does it produce, and is the overflow flag set (1) or not (0)? Enter both.
The hardest one here. Get it, and you've got this lesson.
An 8-bit ALU computes for every one of the pairs of bytes. For how many pairs is the carry out 1? For how many is the signed overflow flag set? Enter both.
Where it's used
Every instruction set's sub, neg and compare instructions run on the same adder as add, with the operand inverted and the carry-in set, so a CPU core needs one adder per integer pipeline, not two. Compilers lean on it too: subtracting a constant is emitted as adding its negation.