digiwleeaLearn
Open the lab →

Encoders and demultiplexers

The duals of the decoder and mux

5 min read

An encoder turns a one-hot set of input lines into the binary address of the active line, the reverse of a decoder. A demultiplexer routes one data input to one of several outputs chosen by a select code, the reverse of a multiplexer.

You have built two routing blocks: the multiplexer (many inputs, one output, a select picks which input passes) and the decoder (an address in, one of many lines raised). Each has a mirror image that does the opposite job. The decoder's reverse is the encoder; the mux's reverse is the demultiplexer (demux). Knowing all four means you can move a signal from anywhere to anywhere, which is most of what a CPU's wiring does.

The demultiplexer: one input, routed to one output

A multiplexer gathers: several data inputs collapse to one output. A demultiplexer scatters: one data input D is sent out on exactly one of several outputs, chosen by a select code. With a 2-bit select S1 S0 you get a 1-to-4 demux: D appears on Y0, Y1, Y2, or Y3 depending on the select, and the other three outputs read 0.
The build is a decoder plus a row of AND gates. The decoder turns the select code into a one-hot line, and each output is that line ANDed with the data D. Only the selected line is 1, so only that output can pass D; the rest are forced to 0.
Yk = (decoder line k) D (D reaches only the selected output)
A 1-to-4 demultiplexer: a decoder turns S1 S0 into a one-hot line, and each output is that line ANDed with the data D. Open it in the lab, set D = 1, and step the select S1 S0 through 00, 01, 10, 11: the single 1 moves across Y0 to Y3, and every other output stays 0. Set D = 0 and all outputs go 0.
A mux and a demux make a complete one-wire link. The mux end picks one of several sources onto a single shared wire; the demux end fans that wire back out to one of several destinations. A decoder is just the special case of a demux whose data input is held at 1, which is why the same decoder block appears inside both.

The encoder: one-hot back to an address

A decoder takes an address and lights one line. An encoder does the reverse: given a set of lines with exactly one high (a one-hot input), it outputs the binary address of that line. A 4-to-2 encoder has four inputs Y0-Y3 and produces a 2-bit address A1 A0: if Y2 is the high one, the output is 10 (binary 2).
Each output address bit is an OR of the input lines whose number has a 1 in that bit position. For 4-to-2: bit A1 is 1 for inputs 2 and 3, and bit A0 is 1 for inputs 1 and 3. So:
A1 = Y2 Y3 A0 = Y1 Y3 (Y0 high, or nothing high, reads as address 00)
A 4-to-2 encoder: A1 = Y2 OR Y3, A0 = Y1 OR Y3 (the Y0 line is the all-zero address, so it needs no gate). Open it in the lab and raise one input at a time: Y1 gives A1 A0 = 01, Y2 gives 10, Y3 gives 11. It reads off the number of the active line.
A plain encoder assumes its input is truly one-hot: exactly one line high. Raise two inputs at once and the ORs blend their addresses into a wrong number (raise Y1 and Y2 and you get A1 A0 = 11, which is neither). Real designs use a priority encoder that, when several lines are high, reports the highest-numbered one and ignores the rest. The plain version here is correct only for one-hot inputs, exactly what a decoder produces.
Try it
On the 4-to-2 encoder, you raise Y3 (and only Y3). What is A1 A0? Now think about the round trip: if you feed that address into a 2-to-4 decoder, which output does it raise?
With the mux, decoder, demux, and encoder, you have the full set of selection and routing blocks. Encoders compress one-hot signals (like turning which interrupt fired into a number); demuxes steer one value to a chosen destination (like writing the data bus into one addressed cell). Next you widen storage from one bit to a whole byte, the 8-bit register, then start hanging blocks on a shared bus.

Frequently asked

What is a demultiplexer?

A demultiplexer (demux) routes one data input to exactly one of several outputs, chosen by a select code, the reverse of a multiplexer. A 1-to-4 demux sends D to Y0, Y1, Y2, or Y3 by the 2-bit select, with the other outputs held at 0. It is built from a decoder and a row of AND gates.

What is an encoder?

An encoder is the reverse of a decoder: given a one-hot set of lines (exactly one high), it outputs the binary address of the active line. A 4-to-2 encoder turns Y0-Y3 into a 2-bit number, each output bit being an OR of the input lines whose number has a 1 in that position.

What is the difference between a decoder and an encoder?

They are inverses. A decoder takes a binary address and raises one of many output lines (address in, one-hot out). An encoder takes a one-hot set of lines and outputs the address of the active one (one-hot in, address out). Encoder then decoder returns the same hot line.

What is a priority encoder?

A plain encoder assumes its input is one-hot; if two lines are high it blends their addresses into a wrong number. A priority encoder fixes that by reporting the highest-numbered active line and ignoring the rest, so it gives a sensible answer even when several inputs are high.

Every lesson here builds toward one thing: a working CPU, from the transistor up.

Open the free lab →
Found this useful? Share itShare on X