Encoder
A digital encoder is a logic circuit that compresses a large number of input lines down into a much smaller number of output lines, assigning a unique binary code to each active input — the direct inverse of a decoder, which does exactly the opposite conversion. A standard 8-to-3 encoder takes 8 separate input lines and produces a 3-bit binary code identifying which single input is currently active, reducing eight distinct wires down to three, because three bits is exactly enough combinations to uniquely identify eight possible sources. How an encoder actually compresses many active input lines down into a compact binary code looks like this:
A few things are worth knowing about why a basic encoder's simplicity breaks down the moment more than one input can be active, and what the real fix looks like:
A basic encoder's logic relies on an unstated assumption — that exactly one input is active at a time — and the moment that assumption is violated, a basic encoder produces a meaningless, ambiguous output rather than a useful one. With simple combinational logic wiring each input directly into the output code, activating two inputs simultaneously causes their individual binary codes to combine incorrectly, producing an output that doesn't correspond to either input and carries no usable meaning at all. This isn't a rare edge case to design around after the fact — many real encoder applications, like a keyboard where a user might genuinely press two keys at once, or an interrupt controller where multiple hardware events can occur simultaneously, guarantee that multiple inputs will sometimes be active together, which means a basic encoder is frequently the wrong tool unless something more sophisticated handles that overlap.
A priority encoder solves the multiple-active-input problem by assigning each input a fixed rank and always outputting the code for the highest-priority active input, deliberately ignoring any lower-priority inputs that happen to be active at the same time. Rather than trying to represent multiple simultaneous events at once, which the limited output width makes impossible anyway, a priority encoder makes a deliberate, predictable choice: if inputs 3 and 5 are both active and input 5 has been assigned higher priority, the output is always the code for input 5, full stop, regardless of what else happens to be active alongside it. This is exactly the structure behind a processor's interrupt controller, where multiple hardware devices can request attention simultaneously but the processor can only service one interrupt at a time — the priority encoder inside the interrupt controller deterministically picks which device gets serviced first, with the others waiting their turn or being re-evaluated on the next cycle.
An encoder's compression only works because the number of output bits is chosen to exactly match how many distinct inputs actually need identifying, which is why encoder size scales logarithmically rather than linearly with input count — the same scaling relationship a multiplexer's select lines follow. Three output bits can represent eight distinct codes, which is exactly enough to identify any one of eight inputs; going from 8 inputs to 16 only requires one additional output bit, not a doubling of output wires, because each additional bit doubles the number of representable codes. This efficient scaling is precisely why an encoder is the standard tool whenever a system needs to identify which one of many possible sources triggered an event using as few signal wires as possible — exactly the situation a keyboard matrix, an interrupt controller, or a sensor array with many discrete channels all face.
Keyboard matrix scanning is one of the most common real-world applications of encoding logic, converting a grid of individual key switches into a compact code identifying exactly which key was pressed, without needing one dedicated wire per key. Wiring a dedicated signal line for every single key on a keyboard would require dozens of separate connections; instead, keys are arranged in a row-and-column matrix, and scanning logic — closely related to encoder and priority-encoder behavior — identifies which specific row-and-column intersection closed when a key was pressed, converting that physical position into a compact digital key code using far fewer wires than one-per-key wiring would ever need. This same underlying principle — compress many physical sources down to a small, unique digital identifier — reappears constantly throughout digital system design, which is exactly why understanding basic and priority encoding pays off well beyond any single specific application.
Read an encoder as a compression circuit built on an assumption, not a universal input-identifying tool: a basic encoder only works correctly when exactly one input is active, a priority encoder exists specifically to make that assumption safe to break by deterministically resolving ties, and the resulting compact binary code is exactly what lets systems like interrupt controllers and keyboard matrices identify one event among many using far fewer wires than a one-signal-per-source approach would ever require.