Computing Library › Classical Logic Gates
Classical Logic Gates

Decoder

A decoder activates exactly one of many outputs according to the binary code on its inputs.

What it does

A decoder converts an n-bit binary input into an activation of one of 2 to the n output lines. For each input code exactly one output is asserted and the rest are inactive, so a decoder turns a compact number into a one-hot selection.

Two-to-four example

A1A0Y0Y1Y2Y3
001000
010100
100010
110001

How it is built

Each output is a single AND term, a minterm, of the input variables. Output Y2, for instance, is A1 AND NOT A0. The decoder is therefore a bank of AND gates, one per output, each fed the right combination of true and complemented inputs.

Enable input

In code

python

def decoder(code, n_bits):
    out = [0] * (1 << n_bits)
    out[code] = 1   # one-hot: only the coded line is 1
    return out

Where it appears

Decoders select memory rows and columns, choose one device from an address, drive seven-segment and matrix displays, and generate control signals from opcode fields. Its inverse, packing a one-hot input back into a binary code, is an encoder.