CE2704 · Digital Logic Design
Theme 4 · Combinational building blocks

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.

A0 A1 E Y0 Y1 Y2 Y3 DEC 2:4
2:4 decoder — input A1A0 lights one of four outputs. (E = enable.)
A1A0active output
00Y0
01Y1
10Y2
11Y3

Each output is the minterm for its code:

$$ Y_2 = A_1\cdot A_0' $$

(active only for the input 10). The other outputs are the other three minterms.

The big use: address decoding. A processor puts an address code on its bus; a decoder turns that code into a single "chip-select" line that wakes exactly one memory chip or peripheral. You'll meet this again inside the microcontroller.
📐 Worked example

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:

$$ 5 = 101_2 \;\Rightarrow\; A_2 = 1,\ A_1 = 0,\ A_0 = 1 $$
$$ Y_5 = A_2\cdot A_1'\cdot A_0 $$
Every decoder output is just the minterm of its index — write the index in binary, and each 1 is the plain variable, each 0 is the complemented variable.

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.

I0 I1 I2 I3 A0 A1 ENC 4:2
4:2 encoder — which input is high becomes a 2-bit code.

For a 4-to-2 encoder (inputs D0…D3, outputs Y1Y0):

$$ Y_1 = D_2 + D_3 \qquad Y_0 = D_1 + D_3 $$

Each output bit is 1 for the input numbers that have that bit set.

The catch: if two inputs are high at once, a plain encoder ORs their codes and outputs a wrong number — e.g. D1 and D2 together give 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?

(a) exactly one output line is 1 at any time. (b) Y1 = A1′·A0 (active for code 01). (c) a decoder. (d) their codes get ORed into a wrong number; a priority encoder (highest input wins, plus a valid bit V) fixes it.

Recap — the whole topic on one screen

BlockDoesKey fact
Decoder (n:2n)code → one active lineone-hot; each output is its minterm (Y2=A1A0′)
Encoder (2n:n)one active line → codereverse of a decoder
Priority encoderhandles many activehighest input wins; adds a valid bit V
Headline useaddress decodingturn a bus address into one chip-select

Next topic

Meet the ALU

You now have every combinational brick: adder, subtractor, comparator, MUX, decoder. Next we bolt them together into the heart of every processor — the arithmetic logic unit.

→ Meet the ALU