← Back to blogPriority encoder

Priority Encoder Explained: 4-to-2 and 8-to-3

Derive 4-to-2 and 8-to-3 priority encoders, use a valid output, cascade groups, and account for glitches, asynchronous requests, and starvation.

DennyDenny
7 min read
Chalkboard feature illustration for “Priority Encoder Explained: 4-to-2 and 8-to-3”

TL;DR: A priority encoder returns the index of the highest-ranked active input and asserts a separate valid signal. A fixed-priority Boolean network resolves simultaneous requests, but it does not store pending requests, guarantee fairness, synchronize asynchronous inputs, or hold a grant. Those behaviors require logic around the encoder.

A plain encoder assumes its input is one-hot. If two inputs are active, its OR equations can produce a code for an input that was never asserted. A priority encoder removes that ambiguity by defining which active input wins.

That makes it useful for interrupt selection, arbitration, find-first-set operations, and event summarization. But “useful in an interrupt controller” is not the same as “is an interrupt controller.” The encoder is a combinational selection primitive; pending bits, masks, handshakes, and fairness are separate design responsibilities.

Define Priority, Polarity, and Validity

Assume four active-high inputs I3I_3 through I0I_0, with the larger index having higher priority:

I3>I2>I1>I0I_3>I_2>I_1>I_0

Outputs O1O0O_1O_0 contain the winning index. V indicates that at least one request exists.

I3I_3I2I_2I1I_1I0I_0O1O0O_1O_0VWinner
0000xx0none
0001001I0I_0
001x011I1I_1
01xx101I2I_2
1xxx111I3I_3

Lower inputs are don’t-cares after a higher input is active. The output code is also a don’t-care when V=0. V is essential because winner 0 and “no winner” would otherwise both appear as 00.

Other interfaces may use:

  • lowest index as highest priority;
  • active-low request inputs;
  • active-low encoded outputs;
  • a defined zero code instead of don’t-care when idle;
  • one-hot grant output in addition to or instead of an index.

The function table, not the block’s name, is the contract.

Why a Plain Encoder Fails

A one-hot 4-to-2 encoder uses:

O1=I3∨I2O_1=I_3\lor I_2 O0=I3∨I1O_0=I_3\lor I_1

If only I2=1I_2=1, the output is 10. If only I1=1I_1=1, it is 01.

But if I2=I1=1I_2=I_1=1, the equations return 11, falsely pointing to I3I_3. The circuit has not malfunctioned; its one-hot input assumption was violated.

A priority encoder defines a result for every many-hot input combination.

Deriving the 4-to-2 Network

One clean derivation forms one-hot winner signals:

W3=I3W_3=I_3 W2=I3‾I2W_2=\overline{I_3}I_2 W1=I3‾ I2‾I1W_1=\overline{I_3}\,\overline{I_2}I_1 W0=I3‾ I2‾ I1‾I0W_0=\overline{I_3}\,\overline{I_2}\,\overline{I_1}I_0

At most one W signal can be high. The binary encoder now operates on a guaranteed one-hot vector:

O1=W3∨W2O_1=W_3\lor W_2 O0=W3∨W1O_0=W_3\lor W_1 V=W3∨W2∨W1∨W0V=W_3\lor W_2\lor W_1\lor W_0

Boolean simplification gives the familiar direct equations:

O1=I3∨I2O_1=I_3\lor I_2 O0=I3∨(I2‾I1)O_0=I_3\lor(\overline{I_2}I_1) V=I3∨I2∨I1∨I0V=I_3\lor I_2\lor I_1\lor I_0

The winner-mask form is often easier to verify and extend. The simplified form may use less logic in a particular implementation.

Scaling to 8-to-3

For inputs I7I_7 through I0I_0, define:

Wi=Ii∧⋀j=i+17Ij‾W_i=I_i\land\bigwedge_{j=i+1}^{7}\overline{I_j}

WiW_i says input i is active and every higher-priority input is inactive.

Then:

V=⋁i=07Wi=⋁i=07IiV=\bigvee_{i=0}^{7}W_i=\bigvee_{i=0}^{7}I_i

The index bits are OR reductions of winner positions whose binary index contains that bit:

O2=W7∨W6∨W5∨W4O_2=W_7\lor W_6\lor W_5\lor W_4 O1=W7∨W6∨W3∨W2O_1=W_7\lor W_6\lor W_3\lor W_2 O0=W7∨W5∨W3∨W1O_0=W_7\lor W_5\lor W_3\lor W_1

This description is exact but a literal linear mask can become deep. Synthesis may factor common terms or use a hierarchical prefix network.

Hierarchical implementation

Split the requests into high and low groups:

high group: I7…I4
low group:  I3…I0

Run a 4-to-2 encoder on each group. If V_high=1, choose the high group’s two-bit result and set O2=1O_2=1. Otherwise choose the low group and set O2=0O_2=0:

O2=VhighO_2=V_{\text{high}} O1:0=Vhigh?Ohigh:OlowO_{1:0}=V_{\text{high}}?O_{\text{high}}:O_{\text{low}} V=Vhigh∨VlowV=V_{\text{high}}\lor V_{\text{low}}

This decomposition exposes the same priority at two levels and scales to wider trees.

The SN74HC148 Convention

The SN74HC148 is an eight-line-to-three-line priority encoder with enable signals intended for cascading. Its interface uses active-low conventions, so an asserted input and its encoded output do not match the active-high equations above without inversion and careful reading of the function table.

When using a physical part:

  • identify every active-low bubble;
  • obey the enable-input condition;
  • use group/enable outputs as specified for cascading;
  • check output drive and fan-out;
  • use maximum, not typical, propagation delays for timing limits.

The logical idea is the same, but polarity errors are among the most common mistakes when moving from a textbook encoder to a 74x148-family device.

Timing, Glitches, and Sampling

A combinational priority encoder has no sampling edge and no memory. When requests change, internal paths settle after different delays. The encoded bits and V can pass through temporary combinations.

For example, if I7I_7 deasserts while I2I_2 is already active, the output should move from 111 to 010. The three result bits need not switch simultaneously in physical hardware.

Choose an interface strategy:

  • register requests, then encode them;
  • register the code and V together after sufficient combinational delay;
  • convert the winner to a one-hot grant and capture it;
  • use a request/grant handshake so the selected requester remains stable;
  • use hazard-aware asynchronous design if there is deliberately no clock.

Do not feed asynchronous external requests directly into a synchronous control path and assume the encoder removes metastability. Each asynchronous request needs an appropriate synchronization or asynchronous-interface scheme. A multi-bit request vector may also need event capture so short pulses are not lost.

Fixed Priority and Starvation

The Boolean network above is fixed priority. If I7I_7 remains asserted, I0I_0 can wait forever.

That may be correct for a safety-critical alarm that must dominate routine traffic. It may be unacceptable for a shared bus.

Fair policies require state:

  • round-robin rotates the first candidate after each grant;
  • aging increases priority with wait time;
  • weighted arbitration gives requesters configured service shares;
  • hierarchical schemes combine class priority with fairness within a class.

A stateless encoder cannot implement those policies by itself.

Where Priority Encoders Fit

Interrupt selection

An interrupt controller can combine:

eligible = pending AND enable AND NOT masked

then priority-encode eligible. The controller must also retain pending events, apply programmable priority rules, route the interrupt, acknowledge service, and handle concurrent arrival and completion. The encoder performs only the “which eligible source ranks first?” step.

Bus arbitration

Requests feed a priority resolver; its one-hot winner can become a grant after being registered. A complete arbiter also defines when ownership begins and ends, how a requester acknowledges the grant, and whether the policy prevents starvation.

Encoding to binary and immediately decoding back to one-hot can be useful when the index must travel through a narrow interface. If only a local grant is needed, a direct one-hot priority network may avoid redundant conversion.

Find-first-set and leading-zero functions

Finding the highest or lowest set bit is a priority-encoding problem. Processor instruction sets may expose count-leading-zero, count-trailing-zero, or bit-scan operations. Their hardware implementation can use hierarchical encoders, arithmetic transformations, lookup structures, or other optimized logic; cycle count and availability are architecture-specific.

Multiple key inputs

A priority encoder can intentionally reduce simultaneous keys to one result. That is appropriate only if losing lower-priority key information is part of the specification. A keyboard that needs multi-key rollover must preserve the full matrix state instead. Diodes address electrical ghost paths; they do not change a priority encoder’s information loss.

Cascading Correctly

Two active-high 4-to-2 blocks can form an 8-to-3 encoder:

VHV_HVLV_LVO2O_2O1O0O_1O_0 source
1x11high group
0110low group
000xdon’t-care

This table makes the priority boundary explicit. It also provides a reusable verification property: low-group input changes must not affect the code while any high-group input is active.

With packaged encoders, use their dedicated cascade signals rather than assuming their V/EO/GS labels and polarities match this abstract block.

Build and Verify It in DigiSim

Start with the 4-to-2 priority encoder template:

  1. Implement W3W_3 through W0W_0.
  2. Confirm the W vector is one-hot-or-zero for all 16 input patterns.
  3. Encode W into O and generate V.
  4. Compare the result with the simplified equations.
  5. Cascade two four-input groups into an eight-input design.

Then verify:

  • no requests gives V=0;
  • every single request returns its own index;
  • every pair returns the higher-priority index;
  • all inputs high returns the configured top priority;
  • low inputs cannot affect the result while a high-group request exists;
  • code and V are captured together if a register is added.

For an eight-input design, exhaustive checking requires only 256 request patterns. Add assertions:

V=⋁IiV=\bigvee I_i

and, whenever V=1, the output index names an asserted input with no asserted input above it.

Functional simulation verifies the Boolean priority rule. It does not prove asynchronous safety, pulse capture, electrical timing, or fairness.

Continue with decoders and encoders for the one-hot/binary relationship, or multiplexers to route data from the selected source.

Reference

Apply the concept in a simulator model.