Fig. 1 — 4-Bit ALU with Status OutputsInteractive simulation

What You'll Learn

  • Perform arithmetic (add, subtract) and logical (AND, OR, XOR) operations on 4-bit operands.
  • Use an operation-select code to route the appropriate functional unit's output.
  • Interpret the displayed Zero and operation-dependent C status flags.
  • Recognize an ALU as a central arithmetic and logic block in a processor datapath.
  • Apply two's complement for subtraction using add-with-inverted-B.

How It Works

This circuit connects eleven switches to one packaged 4-bit Arithmetic Logic Unit (ALU): A0–A3 and B0–B3 form two 4-bit operands, while Op0–Op2 select one of eight operations. Four lamps display R0–R3, and two additional lamps display Zero and C.

The operation code is interpreted as Op2Op1Op0: 000 ADD, 001 SUB, 010 AND, 011 OR, 100 XOR, 101 NOT A, 110 shift A left by one bit, and 111 shift A right by one bit. All results are limited to four bits. Arithmetic therefore wraps modulo 16; shifts discard the bit that leaves the word.

Zero is HIGH exactly when R3R2R1R0 = 0000. The meaning of C depends on the selected operation. ADD uses it as unsigned carry-out, SUB uses it as a borrow flag (C=1 when A

The packaged ALU also defines Negative and Overflow outputs, but this particular circuit does not connect them to lamps. The page therefore distinguishes the component's wider interface from the six signals that the displayed circuit actually exposes.

Try It Step-by-Step

Set the listed controls in the embedded circuit, then compare the observed modeled state with the expected result.

  1. 1
    A=0011B=0010Op=ADD
    Expected:Result = 0101 (= 5), Z=0, C=0
    What you'll see:3 + 2 = 5. The result fits in four bits, so no unsigned carry-out is generated.
  2. 2
    A=0101B=0011Op=SUB
    Expected:Result = 0010 (= 2), Z=0, C=0
    What you'll see:5 − 3 = 2. Under this simulator's borrow-style subtraction convention, C remains LOW because no borrow is required.
  3. 3
    A=1100B=1010Op=AND
    Expected:Result = 1000
    What you'll see:Bitwise AND — only bits where both inputs are 1 stay high. 12 AND 10 = 8.
  4. 4
    A=0011B=1100Op=OR
    Expected:Result = 1111
    What you'll see:Bitwise OR — every bit is set in at least one input, so result is all 1s.
  5. 5
    A=1010B=1010Op=XOR
    Expected:Result = 0000, Z=1
    What you'll see:Bitwise XOR — equal inputs produce 0. Zero flag fires. Useful for equality testing.

Components Used

Applications and Design Context

Processor datapaths. CPUs commonly contain one or more integer ALUs or related execution units. Depending on the architecture and microarchitecture, a particular unit may implement a subset of addition, subtraction, Boolean logic, shifts, and comparison operations. The same word-level semantics extend to 32- or 64-bit operands, while production implementations use different architectures and timing structures.

Programmable controllers. PLCs use simple ALUs for setpoint comparisons, arithmetic on sensor data, and Boolean control logic.

DSP processors. DSPs include specialized ALUs for fixed-point arithmetic plus dedicated multiplier units for sum-of-products operations.

Educational architectures (MIPS, RISC-V). Teaching CPU design typically starts with a small ALU as the first component built and verified.

Hardware accelerators. Custom-designed accelerators (cryptography, signal processing) often have specialized ALUs tuned to their algorithm's specific operations.

Frequently Asked Questions

How is subtraction implemented?

At the arithmetic level, two's-complement subtraction can be expressed as A − B = A + (¬B) + 1. The packaged ALU presents the resulting 4-bit difference and a borrow-style C flag; the template does not expose its internal gate network.

What does the carry flag mean?

For ADD, C is HIGH when the unsigned sum exceeds 15. For SUB in this simulator, C is a borrow flag: it is HIGH when A < B and LOW when no borrow is required. Shift operations use C for the bit shifted out. Always document the convention before using a flag in multiword arithmetic.

Does the displayed C flag indicate signed overflow?

No. C reports unsigned carry, borrow, or a shifted-out bit, depending on the operation. Signed overflow is a separate condition. Although the packaged ALU has additional status outputs, this template connects lamps only to Zero and C.

Why is the zero flag useful?

It's used by conditional branch instructions: "jump if equal" really tests "jump if (A − B) == 0" — i.e., the zero flag from the comparison ALU operation. CPUs use Z as the most-frequently-tested flag.

Why limit to 4 bits in the demo?

Educational tractability. A 4-bit ALU has 256 input combinations per operation — manageable to verify. A 32-bit ALU has 2^64 combinations — needs different verification techniques such as formal methods or simulation with random vectors. The operation semantics carry across widths, but implementation and verification strategy do not.

Continue Learning