Encoders & Decoders

A decoder turns a binary code into a single active line, and an encoder does the reverse. Decoders pick one memory location out of thousands, drive the digits of a seven-segment display and choose which chip on a bus is allowed to talk. Encoders turn a set of buttons or interrupt requests into a compact binary number.

In this lesson you will build a 2-to-4 decoder with an enable input, use a 3-to-8 decoder to implement a logic function, and learn why a plain encoder is not enough and a priority encoder is needed. You should know minterms and the multiplexer lesson first.

Learning objectives

After this lesson you will be able to:

  • Write the truth table and equations of a 2-to-4 decoder with enable.
  • Explain why each decoder output is a minterm of the inputs.
  • Implement a Boolean function with a decoder and an OR gate.
  • Write the truth table of a 4-to-2 encoder and explain its limits.
  • Design and read a priority encoder with a valid output.

The decoder

An n-to-2^n decoder has n input lines and 2^n output lines. For every input code exactly one output is 1 and all the others are 0. A 2-to-4 decoder has inputs A1 and A0, outputs Y0 to Y3, and often an enable input E. When E = 0 all outputs are 0, and when E = 1 the decoder works normally.

E A1 A0 Y0 Y1 Y2 Y3
0 0 0 0 0 0 0
0 0 1 0 0 0 0
0 1 0 0 0 0 0
0 1 1 0 0 0 0
1 0 0 1 0 0 0
1 0 1 0 1 0 0
1 1 0 0 0 1 0
1 1 1 0 0 0 1

The equations are Y0 = E A1'A0', Y1 = E A1'A0, Y2 = E A1 A0' and Y3 = E A1 A0. With E = 1, each output is exactly one minterm of the inputs. Output Yk is 1 when the input code equals k. This is the same as a demultiplexer whose data input is tied to E. Larger decoders follow the same pattern: a 3-to-8 decoder has outputs Y0 to Y7, and a 4-to-16 decoder has 16 outputs.

The enable input is what makes decoders easy to combine. Two 2-to-4 decoders with their enables driven by a third input make a 3-to-8 decoder.

Example 1: reading a decoder

A 3-to-8 decoder has inputs A = 1, B = 0 and C = 1, with A as the most significant bit. Which output is active?

The binary code 101 is 5 in decimal, so output Y5 is 1 and Y0 to Y4, Y6 and Y7 are 0.

Result: only Y5 is active. This one-hot pattern is what makes a decoder useful for selecting one item from many.

Example 2: implementing a function with a decoder

Because every decoder output is a minterm, an OR gate turns a decoder into a function generator. Implement the full adder from the adders lesson with one 3-to-8 decoder and two OR gates. The inputs are A, B and Cin, with A as the most significant bit.

A B Cin Sum Cout Decoder outputs that are 1
0 0 0 0 0 Y0
0 0 1 1 0 Y1
0 1 0 1 0 Y2
0 1 1 0 1 Y3
1 0 0 1 0 Y4
1 0 1 0 1 Y5
1 1 0 0 1 Y6
1 1 1 1 1 Y7

Reading the Sum column, Sum is 1 for minterms 1, 2, 4 and 7. Reading the Cout column, Cout is 1 for minterms 3, 5, 6 and 7.

  • Sum = Y1 + Y2 + Y4 + Y7
  • Cout = Y3 + Y5 + Y6 + Y7

Result: one 3-to-8 decoder and two 4-input OR gates implement the whole full adder. If the decoder has active-low outputs, as many chips do, use NAND gates instead of OR gates.

The encoder

An encoder has 2^n input lines and n output lines. It produces the binary code of the input line that is active. The simplest case is a 4-to-2 encoder, which assumes that exactly one input is 1 at any time.

D3 D2 D1 D0 A1 A0
0 0 0 1 0 0
0 0 1 0 0 1
0 1 0 0 1 0
1 0 0 0 1 1

From the table, A1 = D3 + D2 and A0 = D3 + D1. Notice that D0 does not appear in either equation. When D0 is the active input, the output is 00.

This design has two weaknesses:

  • Ambiguity. With no input active at all, the output is also 00, so you cannot tell "D0 is active" from "nothing is active".
  • Multiple inputs. If D1 and D0 are both 1, the equations give A1A0 = 01. The output claims D1 only, and nothing says that D0 was also pressed.

Example 3: the priority encoder

A priority encoder fixes both problems. It outputs the code of the highest-numbered active input, and a valid output V that is 1 when at least one input is active. In the table, x means "don't care".

D3 D2 D1 D0 A1 A0 V
0 0 0 0 0 0 0
0 0 0 1 0 0 1
0 0 1 x 0 1 1
0 1 x x 1 0 1
1 x x x 1 1 1

The equations are A1 = D3 + D2, A0 = D3 + D2'D1 and V = D3 + D2 + D1 + D0.

Try the input D3D2D1D0 = 0110. Both D2 and D1 are active, but D2 has higher priority. The output is A1A0 = 10 and V = 1.

Result: the priority encoder reports 10 for 0110, and V = 0 tells you when the input is empty, so 00 is never ambiguous.

Priority encoders are used in interrupt controllers, where several devices can ask for attention at the same moment and the processor must serve the most important one first.

Common mistakes

  • Forgetting the enable input. When E = 0 every decoder output must be 0, whatever the address is.
  • Mixing up active-high and active-low outputs. Many decoder chips make the selected output 0 and all others 1.
  • Using a plain encoder with several inputs active. The result is a wrong code. Use a priority encoder.
  • Treating the all-zero input as a valid code. Without a valid output V, the output 00 is ambiguous.
  • Reversing the input order. In a 3-to-8 decoder, ABC = 101 means input 5 only if A is the most significant bit.

Practice problems

Check your answers with the truth table generator.

  1. A 3-to-8 decoder has ABC = 101. Which output is active?

    Answer: Y5, and every other output is 0.

  2. How many inputs and outputs does a 4-to-16 decoder have? How many outputs does an 8-to-3 encoder have?

    Answer: A 4-to-16 decoder has 4 inputs and 16 outputs. An 8-to-3 encoder has 3 outputs.

  3. Implement F(A,B,C) = m(0, 3, 5) with a 3-to-8 decoder.

    Answer: F = Y0 + Y3 + Y5, using one 3-input OR gate. With active-low outputs, use a 3-input NAND gate.

  4. A 4-to-2 priority encoder receives D3D2D1D0 = 0101. Find A1, A0 and V.

    Answer: A1A0 = 10 and V = 1, because D2 is the highest active input.

Frequently asked questions

What is the difference between a decoder and a demultiplexer? A decoder with an enable input is the same circuit as a demultiplexer. The enable acts as the data input. The names reflect how the circuit is used: selecting a location, or routing a signal.

Why do we need a priority encoder? Real systems can have several active requests at once. A plain encoder gives a wrong code in that case, while a priority encoder always reports the most important active input.

Can an encoder and a decoder be used together? Yes. An encoder can compress many request lines into a short binary number that is sent over fewer wires, and a decoder at the other end turns the number back into one active line.

To check your own designs, try the Boolean calculator.

Next: you have now covered the main combinational building blocks. Continue with Sequential Logic and State Machines, where circuits start to remember their past.