4-Bit ALU Simulator (Arithmetic and Logic)
Choose a function with s1, s0 and Cin, as in a digital logic lab ALU, and get the 4-bit output F and carry Cout. Handy for checking your lab results table.
What the circuit does
The arithmetic unit feeds the adder with Y = s0·B + s1·B̅ and computes F = A + Y + Cin. For s1 s0 = 00, 01, 10, 11 the value of Y is 0000, B, B̅ and 1111, so one adder performs increment, add, subtract (A + B̅ + 1) and decrement. The logic unit is a multiplexer that picks OR, XOR, AND or NOT with s1 s0.
Worked example: subtraction 2 − 1
- With s1 s0 = 1 0, Y = B̅ = 1110
- F = A + Y + Cin = 0010 + 1110 + 1 = 1 0001 (17)
- The fifth bit becomes Cout → F = 0001, Cout = 1, so 2 − 1 = 1 (in subtraction, Cout = 1 means no borrow)
Arithmetic function table
| s1 s0 Cin | Operation | F | Cout |
|---|---|---|---|
| 0 0 0 | Transfer (F = A) | 0010 | 0 |
| 0 0 1 | Increment (F = A + 1) | 0011 | 0 |
| 0 1 0 | Add (F = A + B) | 0011 | 0 |
| 0 1 1 | Add with carry (F = A + B + 1) | 0100 | 0 |
| 1 0 0 | A + B̅ (F = A + B̅) | 0000 | 1 |
| 1 0 1 | Subtract (F = A − B) | 0001 | 1 |
| 1 1 0 | Decrement (F = A − 1) | 0001 | 1 |
| 1 1 1 | Transfer (F = A) | 0010 | 1 |
Results for A = 0010 (2) and B = 0001 (1). They match measurements on a breadboard 4-bit ALU.
Logic function table
| s1 s0 | Operation | F (A = 1100, B = 1010) |
|---|---|---|
| 0 0 | OR (F = A ∨ B) | 1110 |
| 0 1 | XOR (F = A ⊕ B) | 0110 |
| 1 0 | AND (F = A ∧ B) | 1000 |
| 1 1 | NOT (F = A̅) | 0011 |
Frequently asked questions
How does an ALU subtract with an adder?
It adds the two's complement: A − B = A + B̅ + 1. The arithmetic unit inverts B (s1 s0 = 10) and sets Cin = 1, so the same adder performs subtraction.
Why is Cout 1 after a subtraction?
When A ≥ B, adding A + B̅ + 1 overflows past 4 bits, so Cout = 1 means no borrow was needed. Cout = 0 after a subtraction means the result went negative.