4-to-2 Priority Encoder
Four-input priority encoder creating 2-bit output. Learn data compression and priority encoding schemes.
Lo que aprenderás
- Encode the highest-priority active input into a binary code.
- Distinguish a priority encoder from a plain encoder.
- Read the priority truth table — multiple inputs active resolves to the highest one.
- Write the priority Boolean expressions: each higher input masks lower ones.
- Apply priority encoders in interrupt controllers and bus arbitration.
Cómo funciona
An encoder is the inverse of a decoder: it converts a one-hot input pattern into a binary code. A priority encoder handles the case where more than one input might be active — it returns the binary index of the highest-priority active input, ignoring lower-priority ones.
This 4-to-2 priority encoder takes 4 inputs (I0–I3, where I3 has highest priority) and produces a 2-bit output (Y1, Y0): - If I3 is active: output 11 (decimal 3) - Else if I2 is active: output 10 (decimal 2) - Else if I1 is active: output 01 (decimal 1) - Else if I0 is active: output 00 (decimal 0) - If all inactive: output 00 plus a separate "valid" bit goes low
A "valid" output (sometimes called V or GS — Group Select) signals whether any input was active. Without it, an output of 00 is ambiguous: it could mean I0 is active or it could mean none are.
Boolean expressions use priority masking: Y1 = I3 + I2 (high if either of the upper two inputs is active); Y0 = I3 + (¬I2 · I1) (high if I3 is active, OR if I1 is active and I2 isn't masking it).
Tabla de verdad
X = don't care. The encoder picks the highest-numbered active input. V (valid) signals whether any input is active.
| Entradas | Salida | ||||||
|---|---|---|---|---|---|---|---|
| I3 | I2 | I1 | I0 | Y1 | Y0 | V | |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | Nothing active — V=0, Y meaningless |
| 0 | 0 | 0 | 1 | 0 | 0 | 1 | Only I0 — code 00, V=1 |
| 0 | 0 | 1 | 0 | 0 | 1 | 1 | Only I1 — code 01 |
| 0 | 0 | 1 | 1 | 0 | 1 | 1 | I1 wins over I0 |
| 0 | 1 | 0 | 0 | 1 | 0 | 1 | Only I2 — code 10 |
| 0 | 1 | 1 | 1 | 1 | 0 | 1 | I2 wins over I1, I0 |
| 1 | 0 | 0 | 0 | 1 | 1 | 1 | Only I3 — code 11 |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | I3 wins over all — highest priority |
Expresión booleana
High bit of code: set if either of the upper two inputs is active.
Low bit: set if I3 is active, OR if I1 is active without I2 masking it.
Valid = OR of all inputs. Tells the consumer whether the encoded output is meaningful.
Pruébalo paso a paso
Configura las entradas en la simulación de arriba, lee qué debería suceder y verifícalo.
- 1I3 = 0 I2 = 0 I1 = 0 I0 = 0Esperado:
Y1Y0=00, V=0Lo que verás: No inputs active — V is low. The output code is 00 but you ignore it because V=0. - 2I3 = 0 I2 = 0 I1 = 0 I0 = 1Esperado:
Y1Y0=00, V=1Lo que verás: Only I0 → code 00 (= 0) and V=1. The encoder confirms valid output even though Y is all zeros. - 3I3 = 0 I2 = 1 I1 = 1 I0 = 1Esperado:
Y1Y0=10, V=1Lo que verás: I0, I1, and I2 all active. I2 has the highest priority so the encoder outputs 10 (= 2). I0 and I1 are ignored. - 4I3 = 1 I2 = 1 I1 = 1 I0 = 1Esperado:
Y1Y0=11, V=1Lo que verás: All inputs active → I3 wins, output 11. Priority encoders always tell you about the highest-numbered active input.
Componentes utilizados
Aplicaciones en el mundo real
Interrupt controllers. Multiple peripheral interrupts arrive simultaneously; a priority encoder picks the highest-priority one and presents its vector to the CPU. Crucial for pre-emptive scheduling.
Cache way replacement. When a cache evicts a line, a priority encoder selects which way to replace based on LRU bits or replacement policy.
Bus arbitration. Multiple bus masters request simultaneously; a priority encoder selects the highest-priority requester to grant the bus to.
Floating-point normalize. After arithmetic, the leading-1 detection of the mantissa uses a priority encoder to determine the shift count for normalization.
Resource allocation. Any "first available slot" search uses a priority encoder to find the lowest-numbered free resource quickly.
Preguntas frecuentes
Why need a priority encoder instead of a plain encoder?
A plain encoder assumes exactly one input is high (one-hot input). If two inputs are high simultaneously, a plain encoder produces an undefined (or arbitrary) output. Priority encoders deterministically pick the highest, even with multiple active inputs.
What's the role of the V (valid) output?
Without V, an output of 00 is ambiguous — it could mean I0 is active or no inputs are active. V=1 confirms 'output is meaningful'; V=0 means 'ignore the encoded output.' Critical for systems that take action only on real input events.
How does this differ from picking the LSB-first instead of MSB-first?
Some priority encoders output the lowest-priority active input (LSB-first). The choice depends on the application — interrupt controllers usually pick highest-priority; resource allocators often pick lowest-numbered. This circuit is MSB-first.
How does the encoder behave with glitches?
If an input bus glitches during transition, the encoded output may briefly show the wrong value. Critical systems either register the encoder output to a flip-flop or require inputs to be stable for one or more clocks before sampling.
Where is this used in CPU interrupt handling?
Multiple interrupt-request lines (IRQ0–IRQ31, etc.) feed a priority encoder; the output is the active interrupt's vector index, used to look up the corresponding interrupt service routine. Hardware does this in nanoseconds — much faster than scanning in software.