G27
Self-checkedTier 1 14 min readdl.decoders-encoders

Decoders, encoders and comparators

Decoders (n to 2^n, with enable, active-low, built from smaller ones), implementing functions with a decoder and OR gates, encoders and the priority encoder, and magnitude comparators.

Why it exists

A decoder turns a binary code into "one wire per value": it is how an address selects one memory row or one register, and how a control unit turns an opcode into an enable signal. An encoder does the opposite, turning "one wire per value" into a code (a keyboard scanner, an interrupt controller reporting which device interrupts). A comparator tells which of two numbers is larger. GATE asks for the output equations, the number of gates, how to build a big decoder from small ones, how to realise a function with a decoder, and how a priority encoder resolves several active inputs.

Prerequisites

You need combinational design and minterms from SOP, POS, minterms and maxterms.

Precise definitions

Decoder (nn-to-2n2^n). Inputs xn−1…x0x_{n-1} \dots x_0 and 2n2^n outputs d0,…,d2n−1d_0, \dots, d_{2^n - 1}. Exactly one output is 1, the one whose index equals the input value: di=mi(x)d_i = m_i(x). A decoder therefore generates all the minterms of its inputs. A 3-to-8 decoder is 8 three-input AND gates and 3 inverters. With an enable input EE, di=E⋅mid_i = E \cdot m_i (all outputs 0 when E=0E = 0). Some decoders are active-low: they are built from NAND gates and the selected output is 0 and the others are 1, di′=(mi)′d_i' = (m_i)'.

Larger decoders from smaller ones. Use the enable inputs: a 4-to-16 decoder is two 3-to-8 decoders; the top input bit drives the enables (enabling the first for 0 and the second for 1, via an inverter) and the other three bits go to both decoders. Equivalently it is five 2-to-4 decoders: one decodes the top two bits and enables one of four others that decode the lower two bits (1+4=51 + 4 = 5). In general an nn-to-2n2^n decoder from kk-to-2k2^k decoders (with n≤2kn \le 2k) needs 1+2n−k1 + 2^{n-k} of them in two levels: one decodes the top n−kn - k bits and enables 2n−k2^{n-k} decoders for the remaining kk bits.

Implementing a function with a decoder. The function is the OR of the minterms where it is 1. Use a decoder and an OR gate on the corresponding outputs. If the decoder is active-low, use a NAND gate on those outputs instead. A circuit with several outputs shares one decoder: the full adder is a 3-to-8 decoder with two OR gates, S=d1+d2+d4+d7S = d_1 + d_2 + d_4 + d_7 and Cout=d3+d5+d6+d7C_{out} = d_3 + d_5 + d_6 + d_7. If a function has more 1s than 0s it is cheaper to OR the minterms where it is 0 and complement.

Encoder (2n2^n-to-nn). Inputs D0,…,D2n−1D_0, \dots, D_{2^n - 1}, outputs a code. An octal-to-binary encoder has A2=D4+D5+D6+D7A_2 = D_4 + D_5 + D_6 + D_7, A1=D2+D3+D6+D7A_1 = D_2 + D_3 + D_6 + D_7, A0=D1+D3+D5+D7A_0 = D_1 + D_3 + D_5 + D_7. It assumes that at most one input is 1. Two limitations: if two inputs are 1 the output is the OR of their codes (wrong), and the code 000 is produced both by D0D_0 and by no input at all (so a valid output VV is needed).

Priority encoder. Resolves several active inputs by choosing the highest-priority one (the largest index by convention) and has an output VV that says whether any input is 1. For 4 inputs with D3D_3 the highest priority:

A1=D3+D2,A0=D3+D2′D1,V=D3+D2+D1+D0.A_1 = D_3 + D_2,\qquad A_0 = D_3 + D_2' D_1,\qquad V = D_3 + D_2 + D_1 + D_0 .

D0D_0 does not appear in A1A_1 or A0A_0: the code for D0D_0 is 00, the same as "nothing", which is why VV is needed.

Magnitude comparator. For one bit: A>BA > B is AB′AB', A=BA = B is A⊙BA \odot B (XNOR), A<BA < B is A′BA'B. For nn bits, let xi=Ai⊙Bix_i = A_i \odot B_i (AiA_i equals BiB_i). Then

(A=B)=x3x2x1x0,(A>B)=A3B3′+x3A2B2′+x3x2A1B1′+x3x2x1A0B0′,(A = B) = x_3 x_2 x_1 x_0,\qquad (A > B) = A_3B_3' + x_3 A_2B_2' + x_3x_2 A_1B_1' + x_3x_2x_1 A_0B_0' ,

and A<BA < B is the same with the roles of AA and BB swapped. The most significant bit that differs decides. Exactly one of the three outputs is 1. Larger comparators are cascaded: the result of the higher group is used and the lower group only matters when the higher group says equal.

How it works

The animation shows the sum bit of a full adder built from a 3-to-8 decoder and one OR gate. The columns are the decoder outputs d1,d2,d4,d7d_1, d_2, d_4, d_7, their OR and the XOR of the inputs; each row has exactly one decoder output active, and the OR of the four matches the sum.

Full-adder sum from a 3-to-8 decoder and an OR gate

Computed exactly, not typed
ABCd1d2d4d7d1+d2+d4+d7S=A xor B xor C
000······
001······
010······
011······
100······
101······
110······
111······

Step 0

Claim: a decoder produces every minterm as a separate output, so the sum bit, which is 1 for minterms 1, 2, 4 and 7, is the OR of those four decoder outputs.

0 / 9
computed by script: exact integer, Fraction and truth-table evaluation (IEEE 754 patterns via Python struct where used), not typed by hand. python3 scripts/gen-dl-traces.py

The priority encoder is easiest to understand from its table. For four inputs, step through all 16 rows: whichever inputs are active, the output is the code of the highest one, and the minimised equation for A0A_0 gives the same answer for every row.

4-to-2 priority encoder (D3 has the highest priority)

Computed exactly, not typed
D3D2D1D0A1 by priorityA0 by priorityV (some input is 1)A1=D3+D2A0=D3+D2'D1
0000·····
0001·····
0010·····
0011·····
0100·····
0101·····
0110·····
0111·····
1000·····
1001·····
1010·····
1011·····
1100·····
1101·····
1110·····
1111·····

Step 0

Claim: when several inputs are 1 the encoder reports the highest-numbered one. The minimised equation A0 = D3 + D2' D1 gives the same bit as the rule for all 16 input rows.

0 / 17
computed by script: exact integer, Fraction and truth-table evaluation (IEEE 754 patterns via Python struct where used), not typed by hand. python3 scripts/gen-dl-traces.py

Worked examples

EX 1Decoder, priority encoder and comparator in a program

Easy
#include <stdio.h>
 
/* 3-to-8 decoder: output i is 1 when the select value is i */
static int decoder(int a, int b, int c, int i) { return (4 * a + 2 * b + c) == i; }
 
/* 4-to-2 priority encoder, D3 highest: returns A1A0 and sets *valid */
static int priority(int d3, int d2, int d1, int d0, int *valid) {
    *valid = d3 || d2 || d1 || d0;
    int a1 = d3 || d2;
    int a0 = d3 || (!d2 && d1);
    (void)d0;
    return 2 * a1 + a0;
}
 
int main(void) {
    int bad = 0;
    for (int m = 0; m < 8; m++) {
        int a = (m >> 2) & 1, b = (m >> 1) & 1, c = m & 1;
        int sum = decoder(a, b, c, 1) || decoder(a, b, c, 2) || decoder(a, b, c, 4) || decoder(a, b, c, 7);
        int carry = decoder(a, b, c, 3) || decoder(a, b, c, 5) || decoder(a, b, c, 6) || decoder(a, b, c, 7);
        bad += sum != (a ^ b ^ c) || carry != (a + b + c >= 2);
    }
    printf("full adder from a decoder and two OR gates, wrong rows: %d\n", bad);
    printf("D3 D2 D1 D0 | A1 A0 V\n");
    for (int m = 0; m < 16; m++) {
        int d3 = (m >> 3) & 1, d2 = (m >> 2) & 1, d1 = (m >> 1) & 1, d0 = m & 1, v;
        int code = priority(d3, d2, d1, d0, &v);
        if (m == 0 || m == 1 || m == 2 || m == 3 || m == 4 || m == 6 || m == 8 || m == 15)
            printf(" %d  %d  %d  %d |  %d  %d  %d\n", d3, d2, d1, d0, code >> 1, code & 1, v);
    }
    int wrong = 0;
    for (int a = 0; a < 16; a++)
        for (int b = 0; b < 16; b++) {
            int x3 = !(((a >> 3) ^ (b >> 3)) & 1), x2 = !(((a >> 2) ^ (b >> 2)) & 1), x1 = !(((a >> 1) ^ (b >> 1)) & 1), x0 = !((a ^ b) & 1);
            int eq = x3 && x2 && x1 && x0;
            int gt = (((a >> 3) & 1) && !((b >> 3) & 1)) || (x3 && ((a >> 2) & 1) && !((b >> 2) & 1)) ||
                     (x3 && x2 && ((a >> 1) & 1) && !((b >> 1) & 1)) || (x3 && x2 && x1 && (a & 1) && !(b & 1));
            wrong += eq != (a == b) || gt != (a > b);
        }
    printf("4-bit comparator equations wrong on %d of 256 pairs\n", wrong);
    return 0;
}
full adder from a decoder and two OR gates, wrong rows: 0
D3 D2 D1 D0 | A1 A0 V
 0  0  0  0 |  0  0  0
 0  0  0  1 |  0  0  1
 0  0  1  0 |  0  1  1
 0  0  1  1 |  0  1  1
 0  1  0  0 |  1  0  1
 0  1  1  0 |  1  0  1
 1  0  0  0 |  1  1  1
 1  1  1  1 |  1  1  1
4-bit comparator equations wrong on 0 of 256 pairs

The first line shows that the decoder with two OR gates gives the full adder on all 8 rows. The table lists some rows of the priority encoder (the highest set input wins; the all-zero row has V=0V = 0). The last line shows the comparator equations are right for all 256 pairs of 4-bit numbers.

EX 2What does a 3-to-8 decoder output?

Easy

For the input ABC=101A B C = 101 the index is 5, so d5=1d_5 = 1 and all the other outputs are 0. If it is active-low, d5=0d_5 = 0 and the rest are 1. It uses 8 AND (or NAND) gates with 3 inputs each and 3 inverters.

EX 3A function with a decoder and an OR gate

Medium

Implement F(A,B,C)=∑m(1,2,4,7)F(A,B,C) = \sum m(1, 2, 4, 7) (the sum bit of a full adder). Connect the decoder outputs d1,d2,d4,d7d_1, d_2, d_4, d_7 to a 4-input OR gate. With an active-low decoder, connect the four low-active outputs to a 4-input NAND gate: F=(d1′d2′d4′d7′)′=d1+d2+d4+d7F = (d_1' d_2' d_4' d_7')' = d_1 + d_2 + d_4 + d_7. The carry Cout=∑m(3,5,6,7)C_{out} = \sum m(3, 5, 6, 7) can share the same decoder with a second OR gate (the program confirms both).

EX 4A 4-to-16 decoder from 3-to-8 decoders

Medium

Use two 3-to-8 decoders. The top input bit x3x_3 goes to the enable of the second decoder and, through an inverter, to the enable of the first; the lower bits x2x1x0x_2 x_1 x_0 go to both. When x3=0x_3 = 0 the first decoder is on and produces d0…d7d_0 \dots d_7; when x3=1x_3 = 1 the second is on and produces d8…d15d_8 \dots d_{15}. Alternatively use five 2-to-4 decoders: one decodes x3x2x_3 x_2 and its four outputs enable four decoders for x1x0x_1 x_0. The two constructions are checked for all 16 inputs.

EX 5Priority encoder: two inputs active

GATE level

D3D2D1D0=0110D_3 D_2 D_1 D_0 = 0110: D2D_2 and D1D_1 are both active. The highest is D2D_2, so A1A0=10A_1 A_0 = 10. The equations: A1=D3+D2=1A_1 = D_3 + D_2 = 1 and A0=D3+D2′D1=0+0⋅1=0A_0 = D_3 + D_2'D_1 = 0 + 0 \cdot 1 = 0, which agrees. V=1V = 1.

For 00000000: A1A0=00A_1 A_0 = 00 and V=0V = 0 (no input active). For 00010001: A1A0=00A_1 A_0 = 00 and V=1V = 1 (D0D_0 is active). The two cases have the same code and are told apart only by VV.

EX 6An ordinary encoder with two inputs active

GATE level

An octal-to-binary encoder has D3=D6=1D_3 = D_6 = 1 and the rest 0. The outputs are the OR of the codes: 011+110=111011 + 110 = 111. The output reads as 7, which is neither input, so an ordinary encoder is only correct when exactly one input is active.

EX 7Comparing two numbers

GATE level

A=1010A = 1010 (10) and B=1001B = 1001 (9). x3=A3⊙B3=1x_3 = A_3 \odot B_3 = 1, x2=1x_2 = 1 (0⊙00 \odot 0), x1=A1⊙B1=1⊙0=0x_1 = A_1 \odot B_1 = 1 \odot 0 = 0, x0=0x_0 = 0. A=BA = B requires all xx to be 1: false. A>BA > B: the terms are A3B3′=0A_3B_3' = 0, x3A2B2′=0x_3 A_2 B_2' = 0, x3x2A1B1′=1⋅1⋅1⋅1=1x_3 x_2 A_1 B_1' = 1 \cdot 1 \cdot 1 \cdot 1 = 1, so A>BA > B. The first differing bit from the left (bit 1) has A1=1A_1 = 1, B1=0B_1 = 0.

Cheat box

Cheat box

  • Decoder n→2nn \to 2^n: di=mid_i = m_i (all minterms). 3-to-8: 8 three-input ANDs and 3 inverters. Enable: di=E mid_i = E\, m_i.
  • Function with a decoder: OR the minterm outputs where F=1F = 1 (NAND if active-low). Full adder: S=d1+d2+d4+d7S = d_1 + d_2 + d_4 + d_7, C=d3+d5+d6+d7C = d_3 + d_5 + d_6 + d_7.
  • 4-to-16 from two 3-to-8 (top bit on the enables) or five 2-to-4.
  • Encoder 2n→n2^n \to n: assumes one active input; D0D_0 and "none" give the same code. Octal: A2=D4+D5+D6+D7A_2 = D_4 + D_5 + D_6 + D_7 and so on.
  • Priority encoder (4 inputs): A1=D3+D2A_1 = D_3 + D_2, A0=D3+D2′D1A_0 = D_3 + D_2'D_1, V=D3+D2+D1+D0V = D_3 + D_2 + D_1 + D_0.
  • Comparator: xi=Ai⊙Bix_i = A_i \odot B_i; A=BA = B: x3x2x1x0x_3x_2x_1x_0; A>BA > B: A3B3′+x3A2B2′+x3x2A1B1′+x3x2x1A0B0′A_3B_3' + x_3A_2B_2' + x_3x_2A_1B_1' + x_3x_2x_1A_0B_0'.
  • DEMUX = decoder with enable as data.

Traps and GATE patterns

Connections

A decoder with a data input on its enable is a demultiplexer, and a decoder followed by an OR gate does the same job as a multiplexer with fixed data (multiplexers and demultiplexers). A decoder driving OR gates is a ROM (an AND plane that is fixed and an OR plane that is programmed): see ROM, PLA and PAL. Comparators build on adders and subtractors (a comparator is a subtractor that discards the difference). In the CPU the control unit uses decoders for the opcode (ALU design).

PYQs

5 previous year questions from your book. I re-solved each one myself (3 by running or simulating, 2 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2026 · Set 2 · NAT · 2 marksQ1 · page 174 of the book
Consider the digital circuit shown below with two
input lines A and B, two select lines S0 and S1, and
an output line Y. The blocks Q and M represent an
active-high 2:4 decoder and a 4-to-1 multiplexer,
respectively.
Out
of
16
possible
input
combinations, the number of combinations that
produce Y = 1 is __________. (Answer in integer).
GATE 2022 · MCQ · 2 marksQ4 · page 174 of the book
Consider a digital display system (DDS) shown in the
figure that displays the contents of register X. A 16-
bit code word is used to load a word in X, either from
S or from R. S is a 1024-word memory segment and
R is a 32-word register file. Based on the value of
mode bit M, T selects an input word to load in X. P
and Q interface with the corresponding bits in the
code word to choose the addressed word. Which one
of the following represents the functionality of P, Q,
and T?

DIGITAL LOGIC
  • (a)P is 10:1 multiplexer;Q is 5: 1 multiplexer; T is 2: 1 multiplexer
  • (b)P is 10: 210 decoder; Q is 5: 25 decoder;T is 2: 1 encoder
  • (c)P is 10:210 decoder; Q is 5: 25 decoder; T is 2: 1 multiplexer
  • (d)P is 1: 10 de-multiplexers; Q is 1: 5 de- multiplexers; T is 2: 1 multiplexer
GATE 2020 · NATQ6 · page 175 of the book
If there are m input lines and n output lines for a
decoder that is used to uniquely address a byte
addressable 1 KB RAM, then the minimum value of
m + n is ____.
GATE 2013 · MCQ · 1 markQ13 · page 176 of the book
In the following truth table, V = 1 if and only if the
input is valid. In the following truth table,    V = 1 if
and only if the input is valid.

Inputs
D0
D1
D2
D3
0
0
0
0
1
0
0
0
x
1
0
0
x
x
1
0
x
x
x
1

Output
X0
X1
V
x
x
0
0
0
1
0
1
1
1
0
1
1
1
1

What function does the truth table represent?
  • (a)Priority encoder
  • (b)Decoder
  • (c)Multiplexer
  • (d)Demultiplexer
GATE 2014 · Set 2 · MCQQ14 · page 186 of the book
Let k = 2n. A circuit is built by giving the output of
an n-bit binary counter as input to an n-to-2n bit
decoder. This circuit is equivalent to a ?
  • (a)k-bit binary up counter
  • (b)k-bit binary down counter
  • (c)k-bit ring counter
  • (d)k-bit Johnson counter DIGITAL LOGIC

Self-check

Question 1

How many 3-to-8 decoders (with enable) are needed to build a 6-to-64 decoder?

Show answer

One decoder decodes the top three bits and drives the enables of eight decoders for the lower three bits: 1+8=91 + 8 = 9.

Question 2

What is the output of a 4-to-2 priority encoder (D3 highest) when D3D2D1D0=1010D_3 D_2 D_1 D_0 = 1010?

Show answer

D3D_3 is the highest active input, so A1A0=11A_1 A_0 = 11 and V=1V = 1. (Check: A1=D3+D2=1A_1 = D_3 + D_2 = 1, A0=D3+D2′D1=1A_0 = D_3 + D_2'D_1 = 1.)

Question 3

Express F=∑m(0,3,5)F = \sum m(0, 3, 5) with a 3-to-8 active-low decoder and one gate.

Show answer

A 3-input NAND gate on the low-active outputs d0′,d3′,d5′d_0', d_3', d_5': F=(d0′d3′d5′)′=d0+d3+d5F = (d_0' d_3' d_5')' = d_0 + d_3 + d_5.

Question 4

For A=0111A = 0111 and B=1000B = 1000, which comparator output is 1?

Show answer

The most significant bit differs first: A3=0A_3 = 0, B3=1B_3 = 1, so A<BA < B is 1 and the other two are 0.

Revision card

One-screen revision

  • Decoder: one output per minterm; enable lets you stack decoders. Function = OR of minterm outputs (NAND if active-low).
  • 4-to-16: two 3-to-8 with the top bit on the enables, or five 2-to-4. 6-to-64: 9 of 3-to-8.
  • Encoder needs one active input; priority encoder picks the highest and has a valid output VV.
  • 4-input priority: A1=D3+D2A_1 = D_3 + D_2, A0=D3+D2′D1A_0 = D_3 + D_2'D_1.
  • Comparator: the first differing bit from the left decides; xi=Ai⊙Bix_i = A_i \odot B_i.

References

  • ·Mano and Ciletti, Digital Design, 5th ed., Chapter 4 Combinational Logic (magnitude comparator, decoders, encoders, priority encoder)
  • ·Kohavi and Jha, Switching and Finite Automata Theory, Building blocks: decoders, encoders, comparators

Verification

Decoder outputs, the 4-to-16 construction from two 3-to-8 or from five 2-to-4 decoders, function realisation with a decoder (active-high and active-low), the simple and priority encoders, the minimal priority-encoder equations and the comparator equations (all 256 pairs of 4-bit numbers) are checked in scripts/enum-checks/dl_comb.py. The animations are computed by scripts/gen-dl-traces.py and checked in tests/dl-traces.test.ts. The C program is compiled and run by npm run verify:c. Textbook chapter titles are from memory; the independent second pass has not been run, so the page stays draft. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (conversions, signed ranges and flags, IEEE 754 fields and custom formats, Boolean identities and counts, K-map groups, adder carries, BCD correction, flip-flop conversions, counter sequences and T equations, state reduction, Quine-McCluskey rounds and covers, floating-point alignment and rounding); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never