HDLbits
Start Practicing

Circuits/Building Larger Circuits

4-bit ALU

medium
aluarithmeticcombinational

Build a 4-bit ALU that performs one of five operations selected by a 3-bit opcode, and reports a carry/borrow-out and a zero flag.

Interface

Signal Direction Width Description
a input 4 Operand A
b input 4 Operand B
opcode input 3 Operation select (see table)
result output 4 ALU result
cout output 1 Carry-out (add/sub only, 0 for logic ops)
zero output 1 1 if result == 0

Opcode encoding

opcode Operation result cout
000 Add a + b carry-out of the addition
001 Subtract a - b (two’s complement: a + ~b + 1) 1 if no borrow (a >= b), 0 if borrow occurred
010 AND a & b 0
011 OR a | b 0
100 XOR a ^ b 0
others undefined 0 0

zero is combinational and simply reflects whether the current result is all zeros, regardless of operation.