Encoders and decoders

Author
Affiliation

Clayton Cafiero and Surya Malik

University of Vermont

Published

2025-09-28

We’ve seen multiplexers, now let’s look at some other combinational circuits that are frequently used: decoders and encoders.

Decoder

A decoder takes in an n-bit binary input and activates a certain output based on values of the input. If our input is n bits, we have 2^n outputs. Essentially, the input tells the circuit which output should be turned on (success, signal of 1 is emitted) and which outputs should be off (blocked, signal of 0 is emitted).

For example, a 2-to-4 decoder has two inputs, B_1 and B_0, and four outputs, Y_3, Y_2, Y_1, and Y_0. Here is the truth table:

B_1 B_0 Y_0 Y_1 Y_2 Y_3
0 0 1 0 0 0
0 1 0 1 0 0
1 0 0 0 1 0
1 1 0 0 0 1

 

Here are the boolean equations for this decoder:

  • Y_0 = \neg B_1 \land \neg B_0 (Y_0 is active when neither B_1 and B_0 are).
  • Y_1 = \neg B_1 \land B_0 (Y_1 is active when B_1 is not active and B_0 is active).
  • Y_2 = B_1 \land \neg B_0 (Y_2 is active when B_1 is active and B_0 is not).
  • Y_3 = B_1 \land B_0 (Y_3 is active when B_1 and B_0 are both active).

Applications for decoders

  • Instruction decoding: The opcode field of an instruction is run through a decoder. That decoder raises control signals that tell the ALU, register file, memory, and multiplexers what to do.
  • Register selection: If a CPU has 32 registers, five bits are needed to pick one. A 5-to-32 decoder turns those five bits into a “one-hot” signal, enabling exactly one register’s read or write port.
  • Memory addressing: You can think of SRAM/DRAM (memory) as laid out in a table with rows and columns. We use row decoders to take the binary row address and activate the one selected row. We use a colum decoder to select a column from within a row.

So decoders are used in instruction decoding, register selection, memory addressing, and elsewhere.

Encoder

An encoder does the reverse of a decoder, taking in 2^n input codes, and producing an n-bit binary output. Encoders can use “one-hot” encoding, where exactly one of the inputs is one and the others are zero, or “few hot encoding” in which more than one input might be active at a given time.

For example, this is the truth table for a one-hot 4-to-2 encoder:

Y_3 Y_2 Y_1 Y_0 B_1 B_0
1 0 0 0 0 0
0 1 0 0 0 1
0 0 1 0 1 0
0 0 0 1 1 1

 

An encoder takes a one-hot (or few-hot) signal and compresses it back into an n-bit code.

Notice that it looks a lot like the truth table for a 2-to-4 decoder—we’ve just swapped the inputs and outputs!

As Boolean formulas:

  • B_1 = Y_1 \lor Y_0
  • B_0 = Y_2 \lor Y_0

Applications of encoders

  • Priority encoders for interrupts: An interrupt is a signal from hardware or software that tells the CPU that something requires immediate attention—an interrupt handler must respond to the event. But all interrupts don’t have the same priority, and multiple interrupts may occur within a given cycle. Encoders can be used to select the the highest-priority request.
  • Bus arbitration: It is often that case that many components share a common bus for exchange of data. Encoders can be used as traffic cops to route traffic on a bus based on priority.
  • Status or condition encoding: The ALU might produce multiple flag lines (zero, carry, overflow, sign). An encoder can compress these into a smaller code for branch control logic.
  • Computer keyboard: While actual details are different, you can think of each key on your keyboard as wired to a separate line. When you press a key, it becomes active and the encoder emits the binary code for that given key. There are multiple inputs, but you only care about the single key you pressed and what should happen when you press that specific key.

The duality between encoders and decoders

Generally, encoders and decoders mirror each other. In a decoder, each output is expressed as a logical AND of input combinations. In an encoder, each output is expressed as a logical OR of one-hot input lines.

© 2025 Clayton Cafiero.

No generative AI was used in writing this material. This was written the old-fashioned way.