CircuitNote

Quick electronics calculators for lab work and circuit design. Free, no sign-up, works in your browser.

Values accept SI prefixes. Examples: 4.7k 4k7 100n 10u 20mA 1M

ALU

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

  1. With s1 s0 = 1 0, Y = B̅ = 1110
  2. F = A + Y + Cin = 0010 + 1110 + 1 = 1 0001 (17)
  3. 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 CinOperationFCout
s1 s0 Cin0 0 0OperationTransfer (F = A)F0010Cout0
s1 s0 Cin0 0 1OperationIncrement (F = A + 1)F0011Cout0
s1 s0 Cin0 1 0OperationAdd (F = A + B)F0011Cout0
s1 s0 Cin0 1 1OperationAdd with carry (F = A + B + 1)F0100Cout0
s1 s0 Cin1 0 0OperationA + B̅ (F = A + B̅)F0000Cout1
s1 s0 Cin1 0 1OperationSubtract (F = A − B)F0001Cout1
s1 s0 Cin1 1 0OperationDecrement (F = A − 1)F0001Cout1
s1 s0 Cin1 1 1OperationTransfer (F = A)F0010Cout1

Results for A = 0010 (2) and B = 0001 (1). They match measurements on a breadboard 4-bit ALU.

Logic function table

s1 s0OperationF (A = 1100, B = 1010)
0 0OR (F = A ∨ B)1110
0 1XOR (F = A ⊕ B)0110
1 0AND (F = A ∧ B)1000
1 1NOT (F = A̅)0011

Using it in practice

Step by step

  1. Enter the two 4-bit operands A and B in binary or decimal.
  2. Set the select lines s1, s0 and the carry-in to choose the operation; the table shows which combination does what.
  3. Read the 4-bit result and the carry-out, and follow the bit-by-bit working.

Common mistakes

  • Forgetting the carry-in for subtraction. A − B is done as A plus the inverted B plus one; that "plus one" usually comes from setting the carry-in.
  • Reading carry as overflow. The carry-out flags an unsigned result too big for 4 bits. For signed numbers (−8 to 7) overflow is a different check: it happens when the carry into the top bit differs from the carry out of it.
  • Reading the borrow backwards. After a subtraction done this way, carry-out 1 means no borrow (A was at least B), and 0 means a borrow occurred.

Building it in the lab

A typical lab ALU combines a 7483 4-bit adder with XOR gates on the B inputs (to invert B for subtraction) and a multiplexer such as the 74153 to choose between arithmetic and logic results. Test it one operation at a time with LEDs on the outputs, and compare each row of your truth table with this simulator before suspecting the chips. Our guide on building a full adder from multiplexers walks through the same parts.

Worked example: 5 + 6 and the overflow flag

Set A = 0101 (5), B = 0110 (6), s1 s0 = 01 and Cin = 0, so F = A + B. Bit by bit from the right: 1 + 0 = 1, 0 + 1 = 1, 1 + 1 = 0 carry 1, then 0 + 0 + 1 = 1. F = 1011 and Cout = 0.

As an unsigned number 1011 is 11, which is right, and Cout = 0 agrees. As a signed number it is −5, which is wrong, because 11 does not fit in −8 to 7. The overflow check confirms it: a carry went into the top bit but none came out of it. LEDs on F alone cannot show this, so wire an XOR of those two carries to a fifth LED if your lab asks for an overflow flag.

Worked example: a subtraction that borrows

Now 2 − 5: A = 0010, B = 0101, s1 s0 = 10 and Cin = 1. The adder sees A + B̅ + 1 = 0010 + 1010 + 1 = 1101 with Cout = 0. Cout = 0 means a borrow happened, so A was smaller than B, and 1101 read as two's complement is −3, the correct answer. When a breadboard ALU gets this row wrong but passes 2 − 1, look at the B inverters and the carry-in wiring first, since those are the only parts that differ from addition. The two's complement calculator converts the 4-bit results back to signed values.

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.