ASBYD0D1D2D3Yr0r1r2r3to ALUO0O1O2O3S1S1S0S001ANDNOT0101OR01AND01010101010101010123MUX 4:10123DEMUX 1:40101010101
Lit wire with moving dashes: it carries a 1Lamps: glowing = 1, dark = 0Gate blocks: NOT, AND, OR; MUX and DEMUX blocks with a switch bladeBlade: points at the input (or output) the select bits choseA multiplexer passes one of its inputs to its output; the select bits choose which. The 2-to-1 mux at the back is built from a NOT gate, two AND gates and an OR gate; the 4-to-1 mux and the demux in front show the choice as a switch blade. A multiplexer chooses one of N data inputs and passes it to its output according to its select signals. A 2:1 mux computes Y = D0·S' + D1·S, the same as Y = (NOT S AND A) OR (S AND B) here (Harris and Harris, Digital Design and Computer Architecture, section 2.8.1). An N:1 multiplexer needs log2(N) select lines: 1 for 2 inputs, 2 for 4, 3 for 8 (Harris and Harris, section 2.8.1). The TI SN74HC153 is a real 4-line to 1-line multiplexer with two select inputs. A demultiplexer sends its one input to the output named by its select bits and holds the other outputs at 0 (Nisan and Schocken, The Elements of Computing Systems, chapter 1: DMux and DMux4Way). The TI SN74HC139 is sold as a 2-line to 4-line decoder/demultiplexer. Processors use muxes to choose the ALU's second operand: a register value or the immediate from the instruction, under a control signal often called ALUSrc (Harris and Harris, section 7.3.1; Patterson and Hennessy, chapter 4). In the Hack computer a Mux16 picks the A register or memory as the ALU input (Nisan and Schocken, chapter 5).

The 2-to-1 mux

  1. A multiplexer, or mux, works like a railway switch for data. Two inputs, A and B, come in and one output, Y, goes out. A select bit, S, sets the switch: it decides which input reaches Y.

Four inputs

Reverse and real uses

The logic

;; step 1/10 · railway-switch
;; about: Select S, Path A, Path B, OR → Y
2-to-1 multiplexer

  inputs:  A, B   (the data)
  select:  S      (which one?)
  output:  Y

  S = 0 → Y = A
  S = 1 → Y = B

Like points on a railway: the trains
(the bits) don't choose; the switch does.

In a real chip each bit is a high or low voltage on a wire; here a glowing lamp shows a 1. A lit wire with moving dashes carries a 1. The switch on the mux is a picture of the logic: the real circuit has no moving parts. In Python, 1 - s turns a bit 0/1 into 1/0 (NOT), & and | on 0 and 1 work as AND and OR, and d[2 * s1 + s0] uses the select bits as a list index.