An encoder is the inverse of a Decoder: it takes a one-hot input ( lines, exactly one asserted) and produces the -bit binary code identifying which line was asserted.
A 4-to-2 encoder takes 4 input lines and outputs 2-bit code :
- → output
- → output
- → output
- → output
The Boolean equations are simple ORs:
is high when an odd-indexed input is asserted; is high when an upper-half input is asserted.

In the diagram, isn’t connected to either output gate — when is the only asserted input, both outputs are (), which is the correct binary code for “input .” This is also the encoder’s fundamental limitation: the output is produced both when and when no input is asserted. The encoder cannot distinguish these cases by itself. That’s why a real encoder almost always carries a separate valid (or “any-input-asserted”) output — without it, you can’t tell “input zero was selected” from “nothing happened.”
Why this is useful
Encoders compress information. A keyboard with 64 keys has 64 individual switches; an encoder converts that to a 6-bit code per keypress, which fits much more comfortably on a bus. Same trick for any peripheral that produces a one-hot signal you need to pack down.
Priority encoder
A plain encoder assumes exactly one input is asserted at a time. If two inputs go high simultaneously, the output is the OR of their codes — usually meaningless.
A priority encoder fixes this. It assigns a priority to each input and outputs the code of the highest-priority input that’s currently asserted. Lower-priority inputs are ignored when a higher-priority one is on.
Common convention: highest input index has highest priority. So if and are both high, the priority encoder outputs the code for .
Priority encoders also typically have a “valid” output — high when at least one input is asserted, low when no inputs are asserted (in which case the binary output is meaningless, since “none” doesn’t have a unique code in -bit binary).
Priority encoders show up in interrupt controllers (the highest-priority pending interrupt is the one the CPU should service next), in floating-point normalization (find the highest set bit in the mantissa), and in arbitration circuits.