Decoders and Encoders
← All articles

Decoders and Encoders

5 min

What a decoder does

A decoder converts a binary code into a "one-hot" signal: given N input bits, it activates exactly one of 2^N output lines — the one whose index matches the binary value of the inputs. A 2:4 decoder has 2 inputs and 4 outputs; a 3:8 decoder has 3 inputs and 8 outputs.

Building a 2:4 decoder from gates

With inputs A and B, each output is simply the AND of the appropriate combination of A, B and their complements:

  • Out0 = ¬A·¬B (active when A=0, B=0)
  • Out1 = ¬A·B (active when A=0, B=1)
  • Out2 = A·¬B (active when A=1, B=0)
  • Out3 = A·B (active when A=1, B=1)

What an encoder does

An encoder is the mirror image: given 2^N one-hot inputs, it outputs the N-bit binary address of whichever input is active. A 4:2 encoder takes 4 inputs and produces a 2-bit code identifying which one is high.

Priority encoders

A plain encoder assumes exactly one input is active at a time — if two inputs go high simultaneously, the output is undefined. A priority encoder resolves this by always outputting the code for the highest-priority (usually highest-indexed) active input, ignoring lower-priority ones. This is exactly what an interrupt controller does: multiple devices can request attention simultaneously, and the priority encoder decides which one gets serviced first.

Where they show up

Decoders select one memory chip or one row of RAM from an address bus; encoders compress sensor or interrupt lines back into a compact binary code. Boolflow ships ready-made Decoder 2:4 / 3:8, Decoder 2:4+EN, Encoder 4:2, and Priority Encoder 8:3 blocks — load a decoder example and toggle the address bits to see exactly one output light up at a time.

Load a decoder example circuit