Decoder & encoder
A decoder turns a compact binary code into one active line; an encoder does the reverse. They're how addresses pick chips and how buttons become numbers.
Built from first principles.
Before you start
What you need first
- The design method — output expressions from a truth table.
- Powers of two — n code bits address 2n lines.
What you'll be able to do
- Explain an n-to-2n decoder and "one-hot".
- Write a decoder output, e.g.
Y₂of a 2:4 decoder. - Explain an encoder, why two active inputs break it, and the priority encoder fix.
The decoder: code in, one line out
An n-to-2n decoder takes an n-bit input code and activates exactly one of its 2n outputs — the one whose number matches the code. "Exactly one output is 1" is called one-hot.
| A1 | A0 | active output |
|---|---|---|
| 0 | 0 | Y0 |
| 0 | 1 | Y1 |
| 1 | 0 | Y2 |
| 1 | 1 | Y3 |
Each output is the minterm for its code:
(active only for the input 10). The other outputs are the other three
minterms.
Output Y₅ of a 3:8 decoder
A 3:8 decoder has inputs A₂A₁A₀ and eight outputs. Output Y5 is
active only when the input code equals 5:
The encoder: the reverse
An encoder goes the other way: given a one-hot input (one of 2n lines active), it outputs the n-bit code of which line is high.
For a 4-to-2 encoder (inputs D0…D3, outputs Y1Y0):
Each output bit is 1 for the input numbers that have that bit set.
11 = 3, which is neither 1 nor 2. A priority encoder fixes this:
it encodes the highest-priority active input and adds a valid output V that is
1 whenever any input is active.
✏️ Try it yourself
(a) What does "one-hot" mean? (b) Write Y1 of a 2:4 decoder. (c) An encoder is the reverse of which block? (d) Why can two active inputs break a simple encoder, and what fixes it?
Recap — the whole topic on one screen
| Block | Does | Key fact |
|---|---|---|
| Decoder (n:2n) | code → one active line | one-hot; each output is its minterm (Y2=A1A0′) |
| Encoder (2n:n) | one active line → code | reverse of a decoder |
| Priority encoder | handles many active | highest input wins; adds a valid bit V |
| Headline use | address decoding | turn a bus address into one chip-select |