Priority Encoder with Valid Flag

Problem Description

ChatGPT Image Aug 1, 2026, 03_46_11 PM

Design a fully combinational priority encoder. Given an 8-bit req bus where any number of bits may be asserted simultaneously, output idx = the bit position of the highest-indexed asserted bit (bit 7 has top priority, bit 0 has lowest). Assert valid whenever any bit of req is high; when req is all-zero, idx should be 3'b000 and valid should be low.

Interface Table

Port

Direction

Width

Description

req

input

8-bit

Request lines; any number can be high

idx

output

3-bit

Combinational, index of highest-priority high bit

valid

output

1-bit

Combinational, high if any req bit is high


Edge Cases

  • req == 8'b0000_0000 → idx = 0, valid = 0

  • Only req[0] high → idx = 0, valid = 1

  • Multiple bits high (e.g. req[3] and req[6] both high) → idx = 6 (higher index wins)

  • All bits high (8'hFF) → idx = 7

All Scenarios

Scenario

Key State

Output fires?

No requests

req == 0

valid = 0 ✗

Single request, low bit

req[0]=1

valid=1, idx=0 ✓

Single request, high bit

req[7]=1

valid=1, idx=7 ✓

Multiple requests, priority resolves

req[2],req[5]=1

valid=1, idx=5 ✓

All requests high

req==8'hFF

valid=1, idx=7 ✓

Hints

  • Hint 1: This is a purely combinational module — no clk or reset port needed at all.

  • Hint 2: Think of it as a priority if-else chain, checked from the HIGHEST bit down to the lowest.

  • Hint 3: valid is simply "OR of all req bits" — you can compute it separately from idx.

  • Hint 4: Structure: one always @(*) block with a priority if-else if-...-else checking req[7] down to req[0], with a final else covering the all-zero case.

Verilog

Code Editor Access

Log in to write, run and submit your solution to this interview question. It's free.

Output

Output goes here...