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.
- 4NAND gates
- Yesfewest possible
- 3inputs
- 1output
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
- A NOT gate on S. 1 when S is 0, meaning "A is selected".
- NAND(A, NOT S). When A is selected this is NOT A; otherwise it stays at 1.
- NAND(B, S). When B is selected this is NOT B; otherwise it stays at 1.
- NANDs the two middle results, which undoes the flip on whichever input was selected.
Truth table
| A | B | S | Q |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 |
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.