HDLbits
Start Practicing

Circuits/Combinational Logic

4-bit Priority Encoder

medium
encoderprioritycombinational

Build a 4-bit priority encoder. Given a 4-bit input in, output the bit position (0-3) of the highest-numbered set bit. If no bits are set, pos is a don’t-care but valid must be driven low.

Interface

Signal Direction Width Description
in input 4 Input vector
pos output 2 Position of the highest set bit
valid output 1 1 if any bit of in is set, 0 if in == 0

Examples

in pos valid
0000 xx 0
0001 00 1
0110 10 1
1010 11 1
1111 11 1

Ties are broken toward the most-significant set bit — e.g. in = 4'b0110 reports position 2, not position 1.