Decoder
A decoder activates exactly one of many outputs according to the binary code on its inputs.
What it does
A decoder converts an n-bit binary input into an activation of one of 2 to the n output lines. For each input code exactly one output is asserted and the rest are inactive, so a decoder turns a compact number into a one-hot selection.
Two-to-four example
| A1 | A0 | Y0 | Y1 | Y2 | Y3 |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 0 | 0 |
| 1 | 0 | 0 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 | 0 | 1 |
How it is built
Each output is a single AND term, a minterm, of the input variables. Output Y2, for instance, is A1 AND NOT A0. The decoder is therefore a bank of AND gates, one per output, each fed the right combination of true and complemented inputs.
Enable input
- An enable line gates all outputs; when disabled every output is inactive.
- With enable, a decoder doubles as a demultiplexer by feeding data to enable.
- Multiple decoders combine with enables to decode wider addresses.
- Active-low variants assert the selected output as 0 instead of 1.
In code
python
def decoder(code, n_bits):
out = [0] * (1 << n_bits)
out[code] = 1 # one-hot: only the coded line is 1
return outWhere it appears
Decoders select memory rows and columns, choose one device from an address, drive seven-segment and matrix displays, and generate control signals from opcode fields. Its inverse, packing a one-hot input back into a binary code, is an encoder.