← All exercises

Priority Encoder

medium

Encodes eight BOOL inputs into the index of the highest-priority active input.

What it teaches

When several fault or request lines are active together, report the highest-numbered one as a compact index from 0 to 7. Sts_Active distinguishes active In_0 from no request. Unlike Bit Pack, one winning input is reported instead of preserving every active bit.

Inputs and outputs

TagTypeDefaultDescription
In_0BOOL
In_1BOOL
In_2BOOL
In_3BOOL
In_4BOOL
In_5BOOL
In_6BOOL
In_7BOOL
Out_IndexDINTNumber of the highest active input
Sts_ActiveBOOL1 while at least one input is active

Required behavior

  • Out_Index is the number of the highest active input: with In_5 and In_2 both on, Out_Index is 5.
  • Sts_Active is 1 while any input is on.
  • With no input active, Out_Index is 0 and Sts_Active is 0 — Sts_Active is what tells In_0 apart from nothing.