Lesson 12 +40 XP

Decoders, Encoders, Priority Encoders & Demultiplexers

Decoders, Encoders, Priority Encoders & Demultiplexers

Digital systems constantly convert between a compact binary code and a one-out-of-many form: an instruction's opcode must activate exactly one operation, a memory address must select exactly one word, and a keyboard must turn "key 6 pressed" into the code 110. Decoders expand an n-bit code into one of 2n lines; encoders do the reverse. This lesson also covers the priority encoder and the demultiplexer, which is a decoder with a data input.

1. Decoders

An n-to-m decoder (m ≤ 2n) has n inputs and up to 2n outputs. For each input combination exactly one output is 1. Each output is therefore a minterm of the inputs: output Di = mi.

2-to-4 decoder with enable

EA1A0D0D1D2D3
0XX0000
1001000
1010100
1100010
1110001
D0 = E A1' A0' quad D1 = E A1' A0 quad D2 = E A1 A0' quad D3 = E A1 A0

The enable input E switches the whole decoder on or off; when E = 0 all outputs are 0. The X entries mean "any value" (they condense several rows into one).

3-to-8 decoder

Three inputs x, y, z and eight outputs D0 = x'y'z' to D7 = xyz: eight 3-input AND gates and three inverters. It is also called a binary-to-octal converter.

2-to-4 decoder with enable, and the 3-to-8 decoder block2-to-4 decoder with enable, and the 3-to-8 decoder block

Active-low decoders (NAND decoders)

Commercial decoders such as the 74138 (3-to-8) and 74139 (dual 2-to-4) are built with NAND gates, so the selected output goes LOW (0) and the others stay HIGH. The enable is also often active-low. In diagrams this is shown with bubbles on the outputs and labels like overline{D0}.

Decoder expansion

Larger decoders are built from smaller ones by using the extra input bits to drive the enable inputs.

SolvedExample 1 · A 4-to-16 decoder from two 3-to-8 decoders

Build a 4-to-16 decoder using two 3-to-8 decoders with enable.

  1. Connect x, y, z (the three low bits) to both decoders.
  2. Connect the MSB w to the enable of the upper decoder through an inverter, and directly to the enable of the lower decoder.
  3. When w = 0 only the upper decoder is enabled and produces D0 ldots D7; when w = 1 only the lower one is enabled and produces D8 ldots D15.
Answertwo 3-to-8 decoders and one inverter; w chooses which decoder is active

In the same way a 5-to-32 decoder needs four 3-to-8 decoders plus a 2-to-4 decoder to drive their enables.

4-to-16 decoder from two 3-to-8 decoders4-to-16 decoder from two 3-to-8 decoders

2. Implementing Functions with a Decoder

Because a decoder generates all minterms, any function of n variables can be built from an n-to-2n decoder and an OR gate that collects the minterms where the function is 1. Several functions of the same inputs share one decoder.

SolvedExample 2 · Full adder with a 3-to-8 decoder

Implement a full adder using a 3-to-8 decoder and OR gates.

  1. From the full-adder truth table: S(x, y, z) = ∑ m(1, 2, 4, 7) and C(x, y, z) = ∑ m(3, 5, 6, 7).
  2. Feed x, y, z to the decoder (x is the MSB).
  3. S = OR of outputs D1, D2, D4, D7; C = OR of outputs D3, D5, D6, D7.
Answerone 3-to-8 decoder and two 4-input OR gates

Tip for functions with many minterms: if F has more 1s than 0s, OR the minterms of F' and use a NOR gate instead (fewer inputs). With an active-low (NAND) decoder, use a NAND gate to collect the active-low minterms of F.

3. BCD to Seven-Segment Decoder

A seven-segment display lights segments a to g to show a digit. The decoder (IC 7447/7448) takes a BCD digit ABCD and outputs one signal per segment. The inputs 1010 to 1111 never occur, so they are don't-cares.

Seven-segment display segmentsSeven-segment display segments

DigitA B C Dabcdefg
000001111110
100010110000
200101101101
300111111001
401000110011
501011011011
601101011111
701111110000
810001111111
910011111011
SolvedExample 3 · Segment a

Find the minimal expression for segment a of a BCD to seven-segment decoder.

  1. a = ∑ m(0, 2, 3, 5, 6, 7, 8, 9) + ∑ d(10, 11, 12, 13, 14, 15).
  2. With the don't-cares, rows AB = 11 and AB = 10 are all 1 or X: the octet gives A.
  3. Columns CD = 11 and CD = 10 (3, 2, 7, 6, 15, 14, 11, 10) form an octet: C.
  4. Cells 5, 7, 13, 15: BD. Corner cells 0, 2, 8, 10: B'D'.
Answera = A + C + BD + B'D'

The other six segments are designed the same way, each with its own K-map.

4. Encoders

An encoder does the inverse of a decoder: it has 2n (or fewer) inputs and n outputs, and outputs the binary code of the one input that is 1.

Octal-to-binary (8-to-3) encoder

Active inputxyz
D0000
D1001
D2010
D3011
D4100
D5101
D6110
D7111

Each output is 1 for the inputs whose code has a 1 in that position, so the encoder is just three 4-input OR gates:

x = D4 + D5 + D6 + D7 qquad y = D2 + D3 + D6 + D7 qquad z = D1 + D3 + D5 + D7

Two limitations:

  • If two inputs are 1 at the same time, the output is wrong: D3 and D6 together give 011 + 110 = 111, which says D7.
  • If no input is 1, the output is 000, the same as for D0.

The priority encoder fixes both.

5. Priority Encoder

A priority encoder outputs the code of the highest-priority active input and ignores the rest. It also has a valid output V that is 1 only when at least one input is active.

4-to-2 priority encoder (D3 highest)

D0D1D2D3xyV
0000XX0
1000001
X100011
XX10101
XXX1111

An X on an input means "whatever its value": a higher input overrides it. Expanding the condensed table and simplifying with K-maps:

x = D2 + D3 qquad y = D3 + D1 D2' qquad V = D0 + D1 + D2 + D3

K-maps and circuit of the 4-to-2 priority encoderK-maps and circuit of the 4-to-2 priority encoder

Reading y = D3 + D1 D2': the LSB of the output is 1 for input 3 (code 11) and input 1 (code 01). Input 1 wins only if input 2 is not active (input 3 is already covered by the first term). The IC 74148 is an 8-to-3 priority encoder with active-low inputs and outputs; it is used for keyboard encoding and interrupt priority in CPUs.

6. Demultiplexer

A demultiplexer (DEMUX) takes one data input and sends it to one of 2n outputs, chosen by n select lines. It is a "data distributor", the reverse of a multiplexer (next lesson).

1-to-4 demultiplexer

S1S0Y0Y1Y2Y3
00D000
010D00
1000D0
11000D
Y0 = D S1' S0' quad Y1 = D S1' S0 quad Y2 = D S1 S0' quad Y3 = D S1 S0

Compare these with the 2-to-4 decoder with enable: they are identical if the enable E is renamed D. So a decoder with an enable input is a demultiplexer, and ICs are sold as "decoder/demultiplexer" (74138, 74155).

1-to-4 demultiplexer1-to-4 demultiplexer

7. Decoders and Encoders in Verilog

The model below uses a shift for the decoder (1 << a puts a single 1 in position a), builds a full adder from it (Example 2), and writes the priority encoder with casez, where ? means "don't care":

VERILOG
// 3-to-8 decoder with enable, a full adder built from it, and a 4-to-2 priority encoder
module decoder3to8 (
    input  [2:0] a,
    input        en,
    output [7:0] d
);
    assign d = en ? (8'b0000_0001 << a) : 8'b0000_0000;
endmodule

module fa_decoder (
    input  x, y, z,
    output s, c
);
    wire [7:0] m;
    decoder3to8 dec (.a({x, y, z}), .en(1'b1), .d(m));
    assign s = m[1] | m[2] | m[4] | m[7];   // S = sum of minterms 1, 2, 4, 7
    assign c = m[3] | m[5] | m[6] | m[7];   // C = sum of minterms 3, 5, 6, 7
endmodule

module prio_enc4 (
    input  [3:0] d,
    output reg [1:0] y,
    output reg       v
);
    always @(*) begin
        v = 1'b1;
        casez (d)
            4'b1???: y = 2'b11;
            4'b01??: y = 2'b10;
            4'b001?: y = 2'b01;
            4'b0001: y = 2'b00;
            default: begin
                y = 2'b00;
                v = 1'b0;
            end
        endcase
    end
endmodule

module tb;
    reg  x, y, z;
    wire s, c;
    reg  [3:0] d;
    wire [1:0] code;
    wire       v;
    integer i;

    fa_decoder  u1 (.x(x), .y(y), .z(z), .s(s), .c(c));
    prio_enc4   u2 (.d(d), .y(code), .v(v));

    initial begin
        $display("x y z | c s");
        for (i = 0; i < 8; i = i + 1) begin
            {x, y, z} = i;
            #1 $display("%b %b %b | %b %b", x, y, z, c, s);
        end
        $display("D3..D0 | y1 y0 v");
        d = 4'b0000; #1 $display("  %b  |  %b  %b", d, code, v);
        d = 4'b0001; #1 $display("  %b  |  %b  %b", d, code, v);
        d = 4'b0110; #1 $display("  %b  |  %b  %b", d, code, v);
        d = 4'b1011; #1 $display("  %b  |  %b  %b", d, code, v);
    end
endmodule

The first table is the full-adder truth table; the second shows that the highest active input wins and that V = 0 when no input is active:

OUTPUT
x y z | c s
0 0 0 | 0 0
0 0 1 | 0 1
0 1 0 | 0 1
0 1 1 | 1 0
1 0 0 | 0 1
1 0 1 | 1 0
1 1 0 | 1 0
1 1 1 | 1 1
D3..D0 | y1 y0 v
  0000  |  00  0
  0001  |  00  1
  0110  |  10  1
  1011  |  11  1
💡 Tip

Key points to remember

  • A decoder's outputs are the minterms of its inputs; decoder + OR gate realises any function.
  • Expand decoders by driving the enables with the extra input bits.
  • Encoder: OR gates; fails for multiple active inputs and cannot tell "no input" from D0.
  • Priority encoder (4-to-2, D3 highest): x = D2 + D3, y = D3 + D1 D2', V = D0 + D1 + D2 + D3.
  • A decoder with enable is a demultiplexer: the enable becomes the data input.
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. What is a decoder? Design a 3-to-8 decoder and use it to implement a full adder. (10 Marks)
  • Write: the truth table, the AND-gate circuit, S = Sigma m(1, 2, 4, 7) and C = Sigma m(3, 5, 6, 7) with OR gates.
  • Q2. Construct a 4-to-16 decoder using two 3-to-8 decoders. (5 Marks)
  • Q3. Design a 4-to-2 priority encoder with a valid output. Give the truth table and K-maps. (10 Marks)
  • Q4. Differentiate between an encoder and a decoder. Explain why a decoder with enable can act as a demultiplexer. (5 Marks)
  • Q5. Design the logic for segment 'a' of a BCD to seven-segment decoder. (5 Marks)