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 (-to-). Inputs and outputs . Exactly one output is 1, the one whose index equals the input value: . 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 , (all outputs 0 when ). Some decoders are active-low: they are built from NAND gates and the selected output is 0 and the others are 1, .
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 (). In general an -to- decoder from -to- decoders (with ) needs of them in two levels: one decodes the top bits and enables decoders for the remaining 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, and . If a function has more 1s than 0s it is cheaper to OR the minterms where it is 0 and complement.
Encoder (-to-). Inputs , outputs a code. An octal-to-binary encoder has , , . 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 and by no input at all (so a valid output is needed).
Priority encoder. Resolves several active inputs by choosing the highest-priority one (the largest index by convention) and has an output that says whether any input is 1. For 4 inputs with the highest priority:
does not appear in or : the code for is 00, the same as "nothing", which is why is needed.
Magnitude comparator. For one bit: is , is (XNOR), is . For bits, let ( equals ). Then
and is the same with the roles of and 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 , 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| A | B | C | d1 | d2 | d4 | d7 | d1+d2+d4+d7 | S=A xor B xor C |
|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | · | · | · | · | · | · |
| 0 | 0 | 1 | · | · | · | · | · | · |
| 0 | 1 | 0 | · | · | · | · | · | · |
| 0 | 1 | 1 | · | · | · | · | · | · |
| 1 | 0 | 0 | · | · | · | · | · | · |
| 1 | 0 | 1 | · | · | · | · | · | · |
| 1 | 1 | 0 | · | · | · | · | · | · |
| 1 | 1 | 1 | · | · | · | · | · | · |
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.
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 gives the same answer for every row.
4-to-2 priority encoder (D3 has the highest priority)
Computed exactly, not typed| D3 | D2 | D1 | D0 | A1 by priority | A0 by priority | V (some input is 1) | A1=D3+D2 | A0=D3+D2'D1 |
|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | · | · | · | · | · |
| 0 | 0 | 0 | 1 | · | · | · | · | · |
| 0 | 0 | 1 | 0 | · | · | · | · | · |
| 0 | 0 | 1 | 1 | · | · | · | · | · |
| 0 | 1 | 0 | 0 | · | · | · | · | · |
| 0 | 1 | 0 | 1 | · | · | · | · | · |
| 0 | 1 | 1 | 0 | · | · | · | · | · |
| 0 | 1 | 1 | 1 | · | · | · | · | · |
| 1 | 0 | 0 | 0 | · | · | · | · | · |
| 1 | 0 | 0 | 1 | · | · | · | · | · |
| 1 | 0 | 1 | 0 | · | · | · | · | · |
| 1 | 0 | 1 | 1 | · | · | · | · | · |
| 1 | 1 | 0 | 0 | · | · | · | · | · |
| 1 | 1 | 0 | 1 | · | · | · | · | · |
| 1 | 1 | 1 | 0 | · | · | · | · | · |
| 1 | 1 | 1 | 1 | · | · | · | · | · |
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.
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 pairsThe 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 ). 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?
EasyFor the input the index is 5, so and all the other outputs are 0. If it is active-low, 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
MediumImplement (the sum bit of a full adder). Connect the decoder outputs to a 4-input OR gate. With an active-low decoder, connect the four low-active outputs to a 4-input NAND gate: . The carry 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
MediumUse two 3-to-8 decoders. The top input bit goes to the enable of the second decoder and, through an inverter, to the enable of the first; the lower bits go to both. When the first decoder is on and produces ; when the second is on and produces . Alternatively use five 2-to-4 decoders: one decodes and its four outputs enable four decoders for . The two constructions are checked for all 16 inputs.
EX 5Priority encoder: two inputs active
GATE level: and are both active. The highest is , so . The equations: and , which agrees. .
For : and (no input active). For : and ( is active). The two cases have the same code and are told apart only by .
EX 6An ordinary encoder with two inputs active
GATE levelAn octal-to-binary encoder has and the rest 0. The outputs are the OR of the codes: . 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(10) and (9). , (), , . requires all to be 1: false. : the terms are , , , so . The first differing bit from the left (bit 1) has , .
Cheat box
Cheat box
- Decoder : (all minterms). 3-to-8: 8 three-input ANDs and 3 inverters. Enable: .
- Function with a decoder: OR the minterm outputs where (NAND if active-low). Full adder: , .
- 4-to-16 from two 3-to-8 (top bit on the enables) or five 2-to-4.
- Encoder : assumes one active input; and "none" give the same code. Octal: and so on.
- Priority encoder (4 inputs): , , .
- Comparator: ; : ; : .
- DEMUX = decoder with enable as data.
Traps and GATE patterns
Connections
Builds on
Unlocks
Across subjects
Within this subject
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
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).
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
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 ____.
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
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 answerHide
One decoder decodes the top three bits and drives the enables of eight decoders for the lower three bits: .
Question 2
What is the output of a 4-to-2 priority encoder (D3 highest) when ?
Show answerHide
is the highest active input, so and . (Check: , .)
Question 3
Express with a 3-to-8 active-low decoder and one gate.
Show answerHide
A 3-input NAND gate on the low-active outputs : .
Question 4
For and , which comparator output is 1?
Show answerHide
The most significant bit differs first: , , so 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 .
- 4-input priority: , .
- Comparator: the first differing bit from the left decides; .
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
My notes (saved in this browser)