ALUCin010101018421A010101018421BINV001101A + BABc····ANDAB····ORAB····00 011011MUXADDSUBANDORs1s0op010101?01?01?01?842101?01?01?01?NZCV
Lit wire with moving dashes: it carries the value the step is aboutLamps: glowing = 1, dark = 0, ? = not worked out yetUnits: adder, AND, OR, each showing its own answer on its frontOp dial and mux: the 2-bit code picks which answer reaches the resultTwo input registers feed an adder, an AND unit and an OR unit at the same time; the op code turns a mux that passes on just one of their answers, and four flag lamps describe the result. The textbook ALU (Harris and Harris, Digital Design and Computer Architecture: ARM Edition, section 5.2.4) has an adder, an AND unit and an OR unit working in parallel, and a 4-input multiplexer that picks one result with a 2-bit control code: 00 ADD, 01 SUB, 10 AND, 11 OR. Real ALUs add more operations, such as XOR, shifts and set-less-than. In two's complement an N-bit number holds −2^(N−1) to 2^(N−1) − 1, so 4 bits hold −8 to 7, and you negate a number by inverting its bits and adding 1. That is why A − B = A + (NOT B) + 1, and one adder can both add and subtract (Harris and Harris, section 1.4.6). N is the top bit of the result and Z is 1 when every result bit is 0. C (the adder's carry-out) and V (signed overflow: both inputs have the same sign and the sum has the other) are only set by ADD and SUB; AND and OR clear them in this ALU (Harris and Harris, figure 5.17).

Inside the ALU

  1. The ALU, short for arithmetic logic unit, is the part of a CPU that does maths and logic. It reads two inputs from registers, small stores of bits. Here A = 0101, which is 5, and B = 0011, which is 3.

Four operations

Status flags

The working

;; step 1/11 · inputs
;; about: Register A, Register B
A = 0101 = 0·8 + 1·4 + 0·2 + 1·1 = 5
B = 0011 = 0·8 + 0·4 + 1·2 + 1·1 = 3

A register is a row of flip-flops that
holds one number. Lit lamp = 1.

In a chip each bit is a high or low voltage on a wire; here a glowing lamp shows a 1. A lit wire with moving dashes is carrying the value the step is about. In Python, ~b & 0b1111 flips the four low bits of b, and &, | work bit by bit on whole numbers. Python's own integers never overflow; the 4-bit limit here is the width of the circuit.