Computer Arithmetic
Integer addition, subtraction, multiplication and division in hardware — overflow detection, Booth's algorithm, and why shifts are cheap.
CPUs work with fixed-width integers (usually 32 or 64 bits). This lesson covers how hardware does arithmetic on them and where the edge cases come from. Start with binary & two's complement if you need a refresher.
Addition and overflow
The same adder handles signed and unsigned numbers. What differs is how you detect overflow:
| Interpretation | Overflow when | Detected by |
|---|---|---|
| Unsigned | The result exceeds 2ⁿ − 1 | Carry out of the top bit (C flag) |
| Signed (two's complement) | Two positives give a negative, or two negatives a positive | Carry into the top bit ≠ carry out (V flag) |
Shifts
- Logical left shift
x << kmultiplies by 2ᵏ (bits fall off the top). - Logical right shift (
>>>in Java/JS) fills with zeros: unsigned division by 2ᵏ. - Arithmetic right shift (
>>) copies the sign bit: signed division by 2ᵏ, but rounding towards −∞ (-7 >> 1 == -4, whereas-7 / 2 == -3).
Shifts take one cycle, so compilers turn x * 8 into x << 3 and x * 10 into (x << 3) + (x << 1).
Multiplication
Binary long multiplication is "shift and add": for each 1 bit in the multiplier, add the shifted multiplicand.
An n-bit × n-bit multiply produces a 2n-bit result. Hardware sums all the partial products at once with a tree of adders (Wallace/Dadda trees), so a 64-bit multiply takes about 3 cycles.
Booth's algorithm
Booth's algorithm handles signed multipliers directly and skips runs of 1s. A run like 0111110 is treated as 1000000 − 0000010: one subtraction and one addition instead of five additions. Look at each pair of adjacent bits (current, previous):
| Pair | Action |
|---|---|
| 0 0 / 1 1 | No operation (inside a run) |
| 1 0 | Start of a run of 1s → subtract the multiplicand |
| 0 1 | End of a run → add the multiplicand |
Then shift arithmetically right and repeat. Radix-4 Booth (looking at 3 bits at a time) halves the number of partial products and is what real multipliers use.
Division
Division is the slow one: each quotient bit depends on the remainder after the previous step, so it can't be fully parallelised. Restoring division:
- Shift the remainder left and bring down the next dividend bit.
- Subtract the divisor.
- If the result is negative, restore (add the divisor back) and the quotient bit is 0; otherwise the bit is 1.
Non-restoring and SRT division avoid the restore step and produce several bits per cycle, but 64-bit division still takes around 20–90 cycles depending on the CPU.
Integer division semantics
| Language | -7 / 2 | -7 % 2 |
|---|---|---|
Java, C, JS (Math.trunc) | −3 (truncate) | −1 |
Python //, % | −4 (floor) | 1 |
Python's % always has the sign of the divisor, which is handy for wrap-around indexing: (i - 1) % n never goes negative. In Java use Math.floorMod.
Saturating and wide arithmetic
- Saturating arithmetic clamps to the maximum or minimum instead of wrapping; it's common in signal and image processing (SIMD instructions support it).
- Multi-precision (big integer) arithmetic chains word-sized operations using the carry flag:
BigInteger, Python ints, JSBigInt.
- One adder serves signed and unsigned values; overflow detection differs (C vs V flag).
- Shifts multiply or divide by powers of two; arithmetic right shift rounds towards −∞.
- Multiplication sums partial products in parallel; Booth's algorithm handles signs and runs of 1s.
- Division is inherently sequential and slow; compilers avoid it for constants.
- Know your language's overflow and division rounding rules.
QHow do you detect overflow when adding two signed ints in Java?medium
Use Math.addExact, which throws. Manually: overflow happened if both operands have the same sign and the result's sign differs, i.e. ((a ^ r) & (b ^ r)) < 0 where r = a + b.
QWhy does Math.abs(Integer.MIN_VALUE) return a negative number?medium
The range is asymmetric: −2³¹ has no positive counterpart in 32 bits. Negating it (invert and add 1) overflows back to −2³¹.