Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 57 ยท Combinational Building Blocks

Decoders, Encoders and Priority Encoders

Understand 2:4 and 3:8 decoders with enable inputs, decoders as minterm generators, encoders, priority encoders and the seven-segment display idea, with Python models.

A multiplexer chooses among inputs. Decoders and encoders translate between two ways of writing the same information. A decoder takes a binary number on n wires and activates exactly one of 2โฟ output lines. An encoder does the reverse: it takes 2โฟ lines, of which one is active, and produces the binary number of the active line. Memory address decoding, instruction decoding, keyboard scanning and interrupt controllers all rely on these two circuits.

In this lesson you will meet the 2:4 and 3:8 decoders, see why a decoder is really a box of minterms, learn what goes wrong with a plain encoder and how a priority encoder repairs it, and glance at the seven-segment display, a decoder that drives light.

Decoders

A 2-to-4 decoder has inputs A1 A0 and outputs Y0 to Y3. Output Yi is 1 exactly when the input number equals i:

Y0 = A1โ€ฒยทA0โ€ฒ, Y1 = A1โ€ฒยทA0, Y2 = A1ยทA0โ€ฒ, Y3 = A1ยทA0

That is four AND gates and two inverters. This is called a 1-of-n (or one-hot) output: at any moment one line is 1 and the rest are 0. A 3-to-8 decoder is the same idea with three inputs and eight three-input AND gates, and in general an n-to-2โฟ decoder needs 2โฟ AND gates of n inputs each.

The enable input

Most decoders have an enable input EN. When EN = 0, every output is 0 no matter what the address says; when EN = 1, the decoder works as normal. Each AND gate simply gets one more input. Enable inputs let you combine small decoders into large ones: a 4:16 decoder is built from one 2:4 decoder that drives the enable pins of four more 2:4 decoders. They are also what lets a decoder act as a demultiplexer, with the enable pin carrying the data.

What a decoder does

In A binary number on n wires.
Out One of 2^n lines turns on.
Enable When off, every line stays 0.

Think of it as turning an address into a light switch.

1 / 2

The Python function below is a model of an n-to-2โฟ decoder with an enable. It prints the full 2:4 table including the disabled rows, then lists all eight rows of a 3:8 decoder with the outputs written as Y7..Y0.

Python 2:4 and 3:8 decoders

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the diagonal of ones: the pattern is the identity matrix, because Yi is the minterm mi of the inputs. That observation leads to one of the most useful facts about decoders.

A decoder is a minterm generator

Each output of an n-to-2โฟ decoder is one minterm of its n inputs. You learned in Sum of Products and Product of Sums that any function is the OR of the minterms where it equals 1. So a decoder plus one OR gate implements any function:

F = ฮฃm(i, j, k, ...) is a decoder with the outputs Yi, Yj, Yk, ... connected to an OR gate.

A full adder has S = ฮฃm(1, 2, 4, 7) and Cout = ฮฃm(3, 5, 6, 7), so one 3:8 decoder and two OR gates are enough. The decoder is shared by both functions, which is why this approach pays off when you need several outputs from the same inputs. (If a function has more than half of its rows equal to 1, use a decoder with inverted outputs and a NAND gate, or use the zeros and complement; it saves inputs on the OR gate.)

Python A full adder from a 3:8 decoder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Decoders appear in real systems under the name address decoding. A memory chip has an address bus; the high address bits go to a decoder, and each decoder output enables one chip or one block of memory. You will use exactly this in ROM, RAM and memory organization.

Encoders

An 8-to-3 encoder has eight input lines D0 to D7 and three outputs Y2 Y1 Y0. Each output is the OR of the inputs whose number has a 1 in that bit position:

  • Y2 = D4 + D5 + D6 + D7
  • Y1 = D2 + D3 + D6 + D7
  • Y0 = D1 + D3 + D5 + D7

If only D5 is active, then Y2 = 1, Y1 = 0, Y0 = 1, which is the binary number 101. A plain encoder has two problems.

  1. More than one active input. If D3 and D4 are both 1, the ORs combine into 111, which names neither input. The output is garbage.
  2. All inputs 0. The output is 000, the same as when only D0 is active, so you cannot tell "nothing pressed" from "line 0 pressed".

Priority encoders

A priority encoder fixes the first problem by agreeing on a rule: if several inputs are active, the one with the highest number wins. It also fixes the second with an extra output V (valid), which is 1 when at least one input is active. For four inputs, the equations are

Y1 = D3 + D2

Y0 = D3 + D2โ€ฒยทD1

V = D3 + D2 + D1 + D0

The term D2โ€ฒ in Y0 is the "priority" part: D1 only counts if D2 is not active. Input D0 produces Y1 Y0 = 00, but V = 1 tells you that it is a real request. Priority encoders are what interrupt controllers use to pick the most urgent request, and what keyboards and arbiters use to pick one winner among many. The program shows both circuits, including the failure of the plain encoder, and the whole 16-row truth table of the priority encoder.

Python Encoder, failure case and priority encoder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In the table, once D3 is 1 the rest of the row is ignored: all eight such rows give 11. The last row is the only one with V = 0, the one case where the code 00 means "nothing".

A decoder that drives light: the seven-segment display

A seven-segment display shows a digit with seven light segments labeled a to g. A circuit that turns a 4-bit binary-coded digit into the seven segment signals is, strictly speaking, a code converter and not a 1-of-n decoder, but it is always called a seven-segment decoder: each segment output is a Boolean function of the four input bits, and you can build it with the methods you have learned (K-maps, or a decoder with OR gates). For the digit 4, segments b, c, f and g are on. The program stores a table of segments for the digits and draws four digits in text. You will derive the logic equations yourself in the seven-segment project.

Python Drawing digits from segment patterns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The binary strings at the bottom (read left to right as a to g) are the truth-table rows that the logic must produce. Digit 7 appears as 1110000: segments a, b and c only.

Practical decoder chips often use active-low outputs: the selected line goes to 0 and the others stay at 1 (the 74138 3:8 decoder works this way). The idea is identical; only the polarity of the outputs is flipped, and a NAND gate replaces the OR when combining outputs.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A 3-to-8 decoder receives the input 101 with its enable set to 1. Which output is 1?

    101 in binary is 5, so output Y5 is the only active line.
  2. 2. Each output of an n-to-2^n decoder corresponds to what?

    Output Yi is the AND of all inputs in true or complemented form, which is minterm mi.
  3. 3. How many 2:4 decoders do you need to build a 4:16 decoder with enable inputs?

    One decoder takes the two upper address bits and enables one of four decoders that take the two lower bits, so 1 + 4 = 5.
  4. 4. Why does a plain 8-to-3 encoder fail when D3 and D4 are both 1?

    Each output is an OR of input lines, so both codes are mixed (011 OR 100 = 111). A priority encoder resolves this by choosing the highest-numbered active input.
  5. 5. What is the purpose of the valid output V on a priority encoder?

    Both situations give Y1 Y0 = 00. V is 1 only when at least one input is active.

Summary

  • An n-to-2โฟ decoder turns a binary number into a one-hot output; each output is a minterm of the inputs.
  • An enable input forces all outputs to 0; it allows decoders to be cascaded and lets a decoder act as a demultiplexer.
  • A decoder plus an OR gate implements any function as a sum of minterms, and several functions can share one decoder.
  • An encoder turns a one-hot input into a binary number but fails if several inputs, or none, are active.
  • A priority encoder lets the highest-numbered active input win and adds a valid output V.
  • A seven-segment decoder is a code converter whose seven outputs are Boolean functions of a 4-bit digit.

Next: Parity and Error Detection.