The ALU
Arithmetic Logic Unit — the calculator
Introduction
Every mathematical calculation, every logical comparison, every decision your computer makes ultimately comes down to one component: the Arithmetic Logic Unit, or ALU. The ALU is the part of the CPU that actually performs the work of addition, subtraction, multiplication, division, and logical operations like AND, OR, NOT, and comparison.
Without the ALU, a computer would be little more than a fancy data-moving machine. Every time you add numbers in a spreadsheet, determine if a game character has collided with a wall, or encrypt a password, the ALU is hard at work. Understanding how the ALU works is key to understanding how computers process information at the most fundamental level.
How It Works
The ALU is a digital circuit built from logic gates that takes two input values (operands) and produces one output result based on a control signal that selects the operation. The ALU's control lines come from the Control Unit, which tells the ALU whether to add, subtract, compare, or perform a bitwise operation on the inputs.
Inside the ALU, different functional units handle different operations. An adder circuit performs addition using a cascade of full adders. A multiplier uses a combination of shifts and additions to compute products. The logic unit handles bitwise AND, OR, XOR, and NOT operations using simple gate arrays. A comparator determines whether one value is greater than, less than, or equal to another.
The ALU also produces flags — status signals that provide information about the result. For example, a zero flag is set when the result is zero, a carry flag is set when an addition produces a carry out of the most significant bit, and an overflow flag indicates when the result exceeds the representable range. These flags are stored in a special flags register and can be tested by conditional branch instructions.
Household Object Analogy
Imagine a pocket calculator that is passed between two people. Person A enters "1234" (first operand). Person B enters "5678" (second operand). The person pressing the buttons selects the operation (add, subtract, multiply). But the calculator doesn't just display the result — it also shows helpful status indicators. If the display shows "0," you know the result was zero. If it shows "E," an overflow or error occurred. The ALU works exactly the same way: it takes two numbers, performs the requested operation, produces the result, and sets status flags that the CPU can check later.
Deeper Dive
The simplest ALU operation is addition, implemented with a ripple-carry adder. A 1-bit full adder takes three inputs (A, B, and carry-in) and produces two outputs (sum and carry-out). To add two N-bit numbers, N full adders are chained together, with each carry-out feeding the next carry-in. While simple, ripple-carry adders become slower as the bit width increases because the carry must propagate through all stages. Modern CPUs use carry-lookahead adders (CLA) that compute all carries in parallel, dramatically reducing addition time.
Subtraction is performed by taking the two's complement of the subtrahend and then adding it to the minuend. The two's complement is computed by inverting all bits and adding 1. This means the CPU can reuse the same adder circuit for both addition and subtraction, with a simple control signal to select the operation.
The flags register (also called the status register) typically includes: Carry (C), Overflow (O/V), Zero (Z), Sign/Negative (S/N), and sometimes Parity (P) and Auxiliary Carry (AC). These flags are critical for implementing conditionals like "if (x > y)" in high-level languages — the compiler generates a comparison instruction (which sets flags), followed by a conditional branch instruction that tests those flags.
Advanced
Modern high-performance CPUs include multiple ALUs operating in parallel. A typical superscalar core might have four or more integer ALUs, two or three floating-point units (FPUs), and dedicated load/store units. The CPU's scheduler dispatches instructions to available ALUs based on data dependencies and resource availability.
Floating-point arithmetic is handled by a specialized unit called the FPU (Floating-Point Unit). Floating-point numbers are represented using the IEEE 754 standard, which stores values in scientific notation with a sign bit, exponent, and mantissa. FPU operations like addition and multiplication require more complex circuitry and typically take several clock cycles. Some CPUs (especially in embedded systems) implement floating-point in software rather than hardware to save silicon area.
The performance of the ALU directly limits the CPU's overall throughput. Bit width matters: a 64-bit ALU can process 64-bit integers in one operation, whereas a 32-bit ALU would need multiple operations for the same task. This is why the transition from 32-bit to 64-bit processors was such a major milestone — it doubled the amount of data the ALU could process per cycle for many operations.
Vocabulary Table
| Term | Definition |
|---|---|
| ALU | Arithmetic Logic Unit — the digital circuit that performs arithmetic and logical operations |
| Adder | A digital circuit that adds two binary numbers, typically using full-adder cells |
| Multiplier | A circuit that computes the product of two binary numbers using shifts and additions |
| Logic Unit | The part of the ALU that performs bitwise AND, OR, XOR, NOT, and shift operations |
| Flags | Status bits (zero, carry, overflow, sign) set by ALU operations for use by conditional instructions |
| Carry | A flag indicating that an addition produced a carry out of the most significant bit |
| Overflow | A flag indicating the result exceeded the representable range for the given data type |
| Comparator | A circuit that compares two values and determines their relative order (>, <, =) |
| FPU | Floating-Point Unit — a specialized ALU for floating-point arithmetic |
| Two's Complement | A binary representation for signed integers used to perform subtraction via addition |
Fun Facts
The 74181, introduced in 1970, was the first complete ALU on a single chip. It could perform 16 arithmetic and 16 logic operations on 4-bit numbers.
A modern CPU can perform over 100 billion integer additions per second in a single core, thanks to deeply pipelined superscalar ALU designs.
The carry-lookahead adder used in modern ALUs was invented by Gerald Rosenberger in 1960 and can compute all carry bits simultaneously using only three gate delays.
The "null" pointers in C and the "NaN" (Not a Number) in floating-point were made possible through clever use of the ALU's ability to detect and flag invalid operations.
Some specialized CPUs (like GPUs) contain thousands of simple ALUs that work in parallel, each capable of basic arithmetic — this is what makes graphics rendering and AI training so fast.
A single XOR gate is one of the most versatile ALU components — it can be used for addition, subtraction, comparison, parity checking, and even basic encryption.
Interactive Diagram
Watch the ALU in action as it adds, subtracts, and compares values while updating the CPU's status flags.
Open Interactive DiagramThe interactive diagram for this chapter demonstrates The Arithmetic Logic Unit. It shows the ALU performing arithmetic (add, subtract) and logical (AND, OR, compare) operations.
What to explore:
- choose an operation; input two numbers; watch the ALU process them and display the result
- the ALU is the part of the CPU that actually performs calculations and logical comparisons on data
Knowledge Check
1. What does ALU stand for?
Answer: Arithmetic Logic Unit
2. Which flag is set when an addition produces a carry out of the most significant bit?
Answer: Carry flag
3. How does a CPU perform subtraction?
Answer: By taking the two's complement of the subtrahend and adding it
