An encoder does the opposite of a decoder: it takes one active line out of many and produces the binary number of that line. A plain binary encoder only works when exactly one input is active. The priority encoder removes that restriction by choosing the highest-priority active input, and it is the form used in real designs for interrupts, arbiters and leading-zero detection.
12 min read | Intermediate Level
What You'll Learn
- How a binary encoder turns one active line into a code, and where it fails
- How a priority encoder resolves several active inputs and flags "nothing active"
- Where priority encoding is used: interrupts, arbiters and leading-zero detection
Quick Answer: Binary vs. Priority Encoder
| Binary encoder | Priority encoder | |
|---|---|---|
| Inputs active at once | Exactly one is assumed | Any number; the highest priority wins |
| Output when several inputs are active | Merged (OR) codes, meaningless | Code of the highest-priority input |
| Output when none is active | Code 0, same as input 0 | Flagged by a valid output V = 0 |
| Logic | One OR gate per output bit | OR gates plus gating for lower priorities |
The Binary Encoder
A 2n-to-n binary encoder has 2n inputs, of which one is assumed active, and n outputs carrying the number of that input. The 4-to-2 case:
| D3 | D2 | D1 | D0 | F1 | F0 |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 0 | 0 |
| 0 | 0 | 1 | 0 | 0 | 1 |
| 0 | 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 0 | 0 | 1 | 1 |
F1 is 1 for inputs D2 and D3, and F0 is 1 for inputs D1 and D3. Each output bit is an OR of the inputs whose code has a 1 in that position. D0 never appears, because its code is 00:
F1 = D3 + D2 F0 = D3 + D1
The 8-to-3 (octal to binary) encoder follows the same rule:
x = D4 + D5 + D6 + D7 (MSB) y = D2 + D3 + D6 + D7 z = D1 + D3 + D5 + D7 (LSB)
Where Are Binary Encoders Used?
Keypads and switch banks, where mechanical design guarantees one active line at a time, feed a binary encoder to produce a key code. Decoder and encoder pairs also compress and expand one-hot control signals between blocks of a chip.
Two Problems With the Plain Encoder
Several inputs active. The OR gates merge the codes. In the 8-to-3 encoder, D3 (011) and D6 (110) together give 011 OR 110 = 111, the code of input 7, which is neither of them.
Nothing active. With all inputs 0, every output is 0. That is also the code for D0, so the output cannot tell "input 0 is active" from "no input is active".
Quick Check
Q: The 8-to-3 binary encoder has D2 and D5 active together. What code does it output?
Show Answer
D2 = 010 and D5 = 101, so the OR of the codes is 111 = 7. Neither D2 nor D5 is reported; this is why a binary encoder needs a one-hot input.
The Priority Encoder
A priority encoder defines what happens when several inputs are active: the highest priority wins and lower ones are ignored. A valid output V is 1 when at least one input is active. Here D3 has the highest priority:
| 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 |
The X entries are don't-cares: once a higher-priority input is 1, the lower ones do not matter. The equations follow from the table:
x = D2 + D3 y = D3 + D1 . D2' V = D0 + D1 + D2 + D3
Check D3 = 0, D2 = 1, D1 = 1, D0 = 0: x = 1, y = 0 + 1.0 = 0 and V = 1, so the code is 10 = 2. D2 wins over D1. When V = 0 the x and y outputs are meaningless.
Quick Check
Q: A 4-input priority encoder (D3 highest) has D3 = 0, D2 = 0, D1 = 1, D0 = 1. What are x, y and V?
Show Answer
D1 is the highest active input, so the code is 01: x = 0, y = 1, V = 1. Check: x = D2 + D3 = 0 and y = D3 + D1.D2' = 0 + 1 = 1.
If the inputs are guaranteed one-hot, a plain OR network is smaller and faster than priority logic, because nothing has to be ranked.
Where Are Priority Encoders Used?
Interrupt controllers pick the most urgent pending request, and bus arbiters pick the highest-priority requester. Floating-point units find the position of the leading 1 of a mantissa (a leading-zero detector) so the result can be shifted into normal form.
Common Mistakes
Mistake #1: Using a plain encoder when several inputs can be active
Wrong: Feeding two request lines into an 8-to-3 binary encoder
Right: Use a priority encoder, or guarantee a single active line.
Why? The OR gates merge the codes, so the output names an input that may not even be active.
Mistake #2: Ignoring the "nothing active" case
Wrong: Using the code outputs without checking V
Right: Qualify the code with V, and ignore it when V = 0.
Why? An all-zero input gives code 0, the same as "input 0 is active".
Mistake #3: Reversing the priority order
Wrong: Masking D3 with the complements of D0 to D2 when D3 should win
Right: Mask each input with the complements of the inputs that rank above it.
Why? The AND with the complements is what ranks the inputs, so the masks decide who has priority.
Practice Exercise
Challenge: Priority Encoder With D0 Highest
Design a 4-input priority encoder where D0 has the highest priority and D3 the lowest.
- Write the truth table with don't-cares.
- Derive the equations for x (MSB), y (LSB) and V.
Hint
The structure mirrors the table above: D1 only counts when D0 = 0, D2 only when D0 = D1 = 0.
Solution
1. D0 D1 D2 D3 | x y V 1 X X X | 0 0 1 0 1 X X | 0 1 1 0 0 1 X | 1 0 1 0 0 0 1 | 1 1 1 0 0 0 0 | X X 0 2. x = D0' . D1' . (D2 + D3) y = D0' . (D1 + D2' . D3) V = D0 + D1 + D2 + D3
Summary
In this tutorial, you learned:
- A binary encoder produces the code of the one active input; each output bit is an OR of the inputs whose code has a 1 there
- It fails when several inputs are active and cannot signal "no input active"
- A priority encoder ranks the inputs and adds a valid output: x = D2 + D3, y = D3 + D1.D2', V = D0 + D1 + D2 + D3
- Each input is masked by the complements of every higher-priority input, which keeps the logic two levels deep