# Encoders and demultiplexers

*The duals of the decoder and mux*

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.

Group: Processor
URL: https://digiwleea.wleeaf.dev/learn/encoder-demux/

You have built two routing blocks: the [multiplexer](https://digiwleea.wleeaf.dev/learn/mux/) (many inputs, one output, a select picks which input passes) and the [decoder](https://digiwleea.wleeaf.dev/learn/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](https://digiwleea.wleeaf.dev/learn/mux/) 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](https://digiwleea.wleeaf.dev/learn/decoder/) plus a row of [AND](https://digiwleea.wleeaf.dev/learn/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) AND D    (D reaches only the selected output)
```

_Circuit diagram: 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._

> **TIP:** 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](https://digiwleea.wleeaf.dev/learn/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](https://digiwleea.wleeaf.dev/learn/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](https://digiwleea.wleeaf.dev/learn/decoder/) 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](https://digiwleea.wleeaf.dev/learn/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 OR Y3      A0 = Y1 OR Y3      (Y0 high, or nothing high, reads as address 00)
```

_Circuit diagram: 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._

> **WARN:** 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](https://digiwleea.wleeaf.dev/learn/decoder/) produces.

**Q (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](https://digiwleea.wleeaf.dev/learn/decoder/), which output does it raise?

**A:** `Y3` high gives `A1 = Y2 OR Y3 = 1` and `A0 = Y1 OR Y3 = 1`, so `A1 A0 = 11` (binary 3). Feeding `11` into a 2-to-4 decoder raises `Y3` and nothing else. Encoder then decoder is a round trip: the encoder turns the hot line into its number, and the decoder turns that number back into the same hot line.

> **KEY:** With the [mux](https://digiwleea.wleeaf.dev/learn/mux/), [decoder](https://digiwleea.wleeaf.dev/learn/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](https://digiwleea.wleeaf.dev/learn/register8/), then start hanging blocks on a shared bus.

### FAQ

**Q:** What is a demultiplexer?

**A:** A demultiplexer (demux) routes one data input to exactly one of several outputs, chosen by a select code, the reverse of a [multiplexer](https://digiwleea.wleeaf.dev/learn/mux/). 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](https://digiwleea.wleeaf.dev/learn/decoder/) and a row of [AND](https://digiwleea.wleeaf.dev/learn/and/) gates.

**Q:** What is an encoder?

**A:** An encoder is the reverse of a [decoder](https://digiwleea.wleeaf.dev/learn/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](https://digiwleea.wleeaf.dev/learn/or/) of the input lines whose number has a `1` in that position.

**Q:** What is the difference between a decoder and an encoder?

**A:** They are inverses. A [decoder](https://digiwleea.wleeaf.dev/learn/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.

**Q:** What is a priority encoder?

**A:** 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.
