Loading lessons...
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
| E | A1 | A0 | D0 | D1 | D2 | D3 |
|---|---|---|---|---|---|---|
| 0 | X | X | 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 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 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.
Build a 4-to-16 decoder using two 3-to-8 decoders with enable.
- Connect x, y, z (the three low bits) to both decoders.
- Connect the MSB w to the enable of the upper decoder through an inverter, and directly to the enable of the lower decoder.
- 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.
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 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.
Implement a full adder using a 3-to-8 decoder and OR gates.
- 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).
- Feed x, y, z to the decoder (x is the MSB).
- S = OR of outputs D1, D2, D4, D7; C = OR of outputs D3, D5, D6, D7.
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 segments
| Digit | A B C D | a | b | c | d | e | f | g |
|---|---|---|---|---|---|---|---|---|
| 0 | 0000 | 1 | 1 | 1 | 1 | 1 | 1 | 0 |
| 1 | 0001 | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
| 2 | 0010 | 1 | 1 | 0 | 1 | 1 | 0 | 1 |
| 3 | 0011 | 1 | 1 | 1 | 1 | 0 | 0 | 1 |
| 4 | 0100 | 0 | 1 | 1 | 0 | 0 | 1 | 1 |
| 5 | 0101 | 1 | 0 | 1 | 1 | 0 | 1 | 1 |
| 6 | 0110 | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
| 7 | 0111 | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
| 8 | 1000 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 9 | 1001 | 1 | 1 | 1 | 1 | 0 | 1 | 1 |
Find the minimal expression for segment a of a BCD to seven-segment decoder.
- a = ∑ m(0, 2, 3, 5, 6, 7, 8, 9) + ∑ d(10, 11, 12, 13, 14, 15).
- With the don't-cares, rows AB = 11 and AB = 10 are all 1 or X: the octet gives A.
- Columns CD = 11 and CD = 10 (3, 2, 7, 6, 15, 14, 11, 10) form an octet: C.
- Cells 5, 7, 13, 15: BD. Corner cells 0, 2, 8, 10: 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 input | x | y | z |
|---|---|---|---|
| D0 | 0 | 0 | 0 |
| D1 | 0 | 0 | 1 |
| D2 | 0 | 1 | 0 |
| D3 | 0 | 1 | 1 |
| D4 | 1 | 0 | 0 |
| D5 | 1 | 0 | 1 |
| D6 | 1 | 1 | 0 |
| D7 | 1 | 1 | 1 |
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:
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)
| D0 | D1 | D2 | D3 | x | y | V |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | X | X | 0 |
| 1 | 0 | 0 | 0 | 0 | 0 | 1 |
| X | 1 | 0 | 0 | 0 | 1 | 1 |
| X | X | 1 | 0 | 1 | 0 | 1 |
| X | X | X | 1 | 1 | 1 | 1 |
An X on an input means "whatever its value": a higher input overrides it. Expanding the condensed table and simplifying with K-maps:
K-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
| S1 | S0 | Y0 | Y1 | Y2 | Y3 |
|---|---|---|---|---|---|
| 0 | 0 | D | 0 | 0 | 0 |
| 0 | 1 | 0 | D | 0 | 0 |
| 1 | 0 | 0 | 0 | D | 0 |
| 1 | 1 | 0 | 0 | 0 | D |
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 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":
// 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
endmoduleThe 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:
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 1Key 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.
📝 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)