Skip to content

Computer Architecture

What is an ALU, and how does it work?

An ALU, or arithmetic logic unit, is the part of a processor that calculates. It takes two numbers and an operation code (opcode) and gives back the result: A + B, A − B, A AND B, A OR B and so on. Inside, it works out every operation it knows at the same time, then the opcode picks which one reaches the output.

Every answer at once

Gates are always on. Feed A and B into an adder, an AND and an XOR, and all three produce their answers at once, whether anyone wants them or not. An ALU doesn't choose which circuit to run; it chooses which answer to pass on.

Here's one bit of a two-operation ALU. OP = 0 means AND, OP = 1 means XOR:

Y = (A · B · NOT OP) + ((A ⊕ B) · OP)

When OP is 0, NOT OP is 1, so the AND's result passes its gate and the XOR's is blocked. When OP is 1 it's the other way round. The OR at the end lets through whichever got past. That chooser is a multiplexer, and it's the heart of every ALU.

Live experiment

One bit of an ALU

A AND B and A XOR B are both worked out all the time. OP opens one result's gate and NOT OP the other's, and an OR passes whichever got through to Y. Flip only OP and the same inputs give a different answer.

Try it yourself: free lesson

“Pick the operation” is lesson 2 of 6 in Register Reef, Computer Architecture World 1. Free with an account. Lessons unlock in order, so you’ll do the one before it first.

More operations, more opcode bits

One OP bit chooses between two results. Two bits make four patterns, 00, 01, 10 and 11, so they can pick from four: say AND, OR, XOR and ADD. Three bits pick from eight. A real ALU's opcode is a few bits wide and chooses among add, subtract, the logic operations, comparisons and shifts.

Subtraction comes almost free. In two's complement, A − B is A + NOT B + 1, so the same adder does both, with an XOR on each B bit to invert it and the carry in set to 1.

Many bits wide

A 32-bit ALU is 32 one-bit slices side by side, all sharing the same opcode. The logic operations work on each bit separately; the adder's slices are joined by a carry chain, from the 1s column upward. The famous 74181 chip of 1970 put a 4-bit ALU in one package, with 16 logic and 16 arithmetic functions.

Where it sits

In the fetch, decode, execute cycle, the decoder turns each instruction's opcode into the ALU's operation code, the registers supply A and B, and the result goes back into a register. Everything a processor computes, from a spreadsheet sum to a pixel's colour, passes through an ALU one operation at a time.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Pick the operation” is lesson 2 of 6 in Register Reef, Computer Architecture World 1. Free with an account. Lessons unlock in order, so you’ll do the one before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3In the arithmetic unit, the OP bit…
    Show the answer

    Answer: Picks which result reaches the output. Every result is made; OP chooses one.

    • Why not “Adds the inputs”? Adding is the adder's job. OP is a selector: it picks between results that are already made.
    • Why not “Clears the output”? Nothing clears the output. OP passes one result to it.
    • Why not “Starts the AND gate”? The gates work on their own as soon as A and B arrive. OP only decides which result gets through.
  2. Question 2 of 3A and B are both 1 and OP is 0, so Y is 1. You flip only OP. Y…
    Show the answer

    Answer: Drops to 0: OP now picks A XOR B, and 1 XOR 1 is 0. Both answers were made all along: A AND B is 1, A XOR B is 0. Flipping OP shuts the AND's gate and opens the XOR's, so Y shows the XOR's 0.

    • Why not “Stays 1”? Y stays 1 only if the newly chosen result is also 1. A XOR B for two 1s is 0.
    • Why not “Goes to 2”? Y is one bit. It can only be 0 or 1, never 2.
    • Why not “Stays 1 until A or B changes”? Y doesn't wait for A or B to change: OP alone switches which result reaches it, at once.
  3. Question 3 of 3Why is ZERO made with a NOR?
    Show the answer

    Answer: A NOR is 1 only when every input is 0. NOR is NOT OR. OR lights if any bit is 1, so NOR lights only when none is: exactly when the number is 0.

    • Why not “A NOR stores a bit”? Storing a bit takes a flip-flop or latch. A single NOR just works out its answer from its inputs.
    • Why not “A NOR is 1 when any input is 1”? That describes OR. A NOR gives the opposite: 0 when any input is 1.
    • Why not “A NOR adds its inputs”? A NOR doesn't add anything; it's NOT OR, a 1 only when all its inputs are 0.
Keep practising: free lesson

“Pick the operation” is lesson 2 of 6 in Register Reef, Computer Architecture World 1. Free with an account. Lessons unlock in order, so you’ll do the one before it first.

Questions people ask

What does the ALU do in a CPU?

It does the arithmetic and logic: add, subtract, AND, OR, XOR, compare and shift. The processor hands it two numbers and an operation code, and it returns the result plus flags such as zero and carry that later instructions can test.

What is the difference between the ALU and the CPU?

The ALU is one part of the CPU, the part that calculates. The CPU also has registers to hold numbers, a program counter, and a control unit that fetches and decodes each instruction and tells the ALU what to do.

How many bits does an ALU have?

As many as the numbers it works on: an 8-bit processor has an 8-bit ALU and a 64-bit processor a 64-bit one. A wide ALU is many one-bit slices side by side, with a carry chain joining them for addition.

Every topic