Skip to content
digiwleeaTools
LearnGlossaryAboutCheat sheetHow a CPU worksToolsOpen the lab →

8-bit ALU from eight 1-bit slices, live

An 8-bit ALU (arithmetic logic unit) applies one of four operations to two 8-bit numbers A and B, chosen by F1 F0: 00 add, 01 subtract, 10 AND, 11 OR. This one is eight copies of the 1-bit ALU slice in a row, with the carry rippling from slice to slice, plus a zero flag Z that is 1 when the result is 0.

Each slice has a full adder whose B input passes through an XOR with F0, so F0 = 1 inverts B, and the carry into bit 0 is F0 as well: A + NOT B + 1 is A minus B in two's complement. F1 picks the adder's sum or the logic result, which F0 switches between AND and OR. Cout is the carry out of the top slice; in a subtraction it is 1 when A is at least B (as unsigned numbers). The Z flag is what a CPU's conditional jump tests. The 1-bit ALU slice and the 4-bit ALU show the same design smaller. Before the page is built, 632 operations are checked, every operation on 158 operand pairs (edge cases such as 255 + 1 and 128 + 128, plus pseudo-random values), against ordinary 8-bit arithmetic.

Big circuit: pinch, scroll or tap + to zoom in, then drag to move around.

Live circuit: click the inputs to toggle them and watch the outputs update. It runs the real simulator, right here on the page.

Open this circuit in the lab →

What you need

Step by step

  1. Place inputs A7 to A0, B7 to B0, F1 and F0, and probes R7 to R0, Cout and Z.
  2. Place eight 1-bit ALU slices. Wire Ai and Bi to slice i, and F1 and F0 to every slice.
  3. Chain the carries: slice 0's carry-in is F0, and each slice's Cout feeds the next slice's Cin. Slice 7's Cout is the ALU's Cout.
  4. Zero flag: OR the eight R outputs together in a tree and invert the result.
  5. Try F1 F0 = 01 with A = B: the result is 0, so Z = 1, and Cout = 1 because A is at least B.

Operations

F1 F0OperationRCout
00ADDA + B (low 8 bits)carry out of bit 7
01SUBA - B, as A + NOT B + 11 when A is at least B (unsigned)
10ANDA AND B, bit by bitthe adder's carry (ignored)
11ORA OR B, bit by bitthe adder's carry (ignored)

Z is 1 whenever R is 00000000, in any operation. Signed overflow is not flagged here; it is the XOR of the carries into and out of bit 7.

You can also read the full theory in the lesson on the ALU. Prefer to build it yourself from scratch? Open the digiwleea lab and place the gates on the canvas, or start from the logic gate simulator.

Build more parts from gates

half adderfull adderhalf subtractorfull subtractor2:1 multiplexer4:1 multiplexerXOR gate from NAND gates2-to-4 decoderSR latchD flip-flopJK flip-flopT flip-flop4-bit shift register4-bit ripple carry adderD latch1-to-4 demultiplexer4-bit magnitude comparator4-bit ring counter4-bit Johnson counter4-to-2 priority encoder4-bit synchronous counter3-to-8 decoder8-to-1 multiplexer4-bit carry lookahead adderhalf adder from NAND gates2-bit comparatorBCD to 7-segment decoder4-bit up/down counterdecade counter2-bit multiplier4-bit ALU4-bit array multiplierNOT, AND, OR and NOR from NANDNOT, OR, AND and XOR from NORfull adder from NAND gatesSR latch from NAND gatesparity generator and checker4-bit adder/subtractorBCD adder4-bit universal shift registermod-6 counter4-bit ripple counterbinary to Gray code converterhalf adder from NOR gatesfull adder from NOR gates4-bit register with parallel load1011 sequence detectorbinary to BCD converterD flip-flop from NAND gates1-bit ALUCMOS inverterCMOS NAND gateCMOS NOR gatePISO shift registerSR flip-flop from NAND gatesBCD to excess-3 code converterHamming (7,4) code encoder and correctortraffic light controller4-bit barrel shifterCMOS AND gateCMOS OR gateCMOS XOR gate1-bit magnitude comparatorhalf subtractor from NAND gatesfull subtractor from NAND gates8-to-3 encoderGray to binary converter4-to-16 decoderfull adder using 4:1 multiplexersedge detectorserial adder4-bit carry-select adderMealy machine (1011 detector)master-slave JK flip-flop (NAND)half subtractor from NOR gates1-to-8 demultiplexerexcess-3 to BCD code convertervending machine FSM4-bit carry-save adder3-bit synchronous counter (JK)CMOS transmission gatetri-state buffer (CMOS)2-to-1 mux from transmission gatesmod-5 counterflip-flop conversion (JK to D)decimal to BCD encoderBCD to decimal decoder4-to-1 mux from 2-to-1 muxesXOR gate from NOR gates

Each of these is one step from a transistor to a working CPU. Build every gate from CMOS transistors and work all the way up in the free digiwleea lab, with the free theory reference alongside.

Open the 8-bit ALU in the lab →

Frequently asked

What is an 8-bit ALU?

The part of a CPU that performs arithmetic and logic on 8-bit numbers: here add, subtract, AND and OR, chosen by two control bits, with a carry out and a zero flag.

How does an ALU subtract?

It adds the two's complement: it inverts every bit of B and sets the carry into the lowest bit to 1, so A + NOT B + 1 equals A minus B. The same adder does both jobs.

What is the zero flag used for?

It tells the CPU whether the last result was 0, so instructions such as jump-if-zero can branch on it; comparing two numbers is a subtraction followed by a look at the flags.

How many 1-bit ALUs make an 8-bit ALU?

Eight, one per bit, chained by their carries like a ripple-carry adder, with the control bits shared by all of them.