NAND Bricks

How to build a 2-to-1 multiplexer from NAND gates

A 2-to-1 multiplexer takes four NAND gates. One inverts the select line S. The second lets A through when S is 0, the third lets B through when S is 1, and the fourth combines them. The output follows A when S is 0 and B when S is 1.

Inputs
1NAND2NAND3NAND4NANDA0B1S11Q
Click the inputs to try every combination. Green wires carry 1 and red wires carry 0. The small numbers match the steps below.
Open this circuit in NAND BricksLoads it as a new board with its truth table, ready to run.

The formula

Q = NAND(NAND(A, NOT S), NAND(B, S))

How it works

A multiplexer is a switch controlled by a signal. With S at 0 the output copies A, and with S at 1 it copies B. Written out, that is (A AND NOT S) OR (B AND S).

In NAND form, each gate in the middle column is a gate that is "closed" unless its select condition holds. NAND(A, NOT S) can only be 0 when S is 0, and then it is just NOT A. NAND(B, S) can only be 0 when S is 1, and then it is NOT B.

The last NAND flips whichever one is active back the right way up. The inactive one sits at 1, which a NAND ignores.

Gate by gate

  1. A NOT gate on S. 1 when S is 0, meaning "A is selected".
  2. NAND(A, NOT S). When A is selected this is NOT A; otherwise it stays at 1.
  3. NAND(B, S). When B is selected this is NOT B; otherwise it stays at 1.
  4. NANDs the two middle results, which undoes the flip on whichever input was selected.

Truth table

ABSQ
0000
0010
0100
0111
1001
1010
1101
1111

Why 4 NAND gates?

Four is the fewest possible. An exhaustive search over every three-gate circuit with three inputs finds no multiplexer.

Where it's used

Multiplexers choose. They pick which register feeds the ALU, which address goes to memory, and which result a CPU keeps. Stack them and you get 4-to-1, 8-to-1 and wider selectors.