I3I2I1I0Y1Y001010101ENCODERPRIORITY0101842101010101BCD → 7A1 A0 → Y3 Y2 Y1 Y00 0 → 0 0 0 10 1 → 0 0 1 01 0 → 0 1 0 01 1 → 1 0 0 0A1A0A1¬A1A0¬A0Y0Y1Y2Y30101NOTNOTAND01AND01AND01AND010x0120x1070x23F0x3000x4A40x5190x65C0x72E0x8080x9FF0xA610xB4B0xC900xD330xE0D0xF7AADDSUBANDOR
Lit wire with moving dashes: it carries a 1Lamps: a glowing lamp shows 1, a dark one 0Gate blocks: the symbol is on top, the name on the frontA dot joins a tap to a rail; wires that cross without a dot do not touchA 2-to-4 decoder turns the 2-bit number on lamps A1 and A0 into exactly one lit output, Y0 to Y3, using two NOT gates and four AND gates. The lit output drives one word line, which selects one memory row. Behind it, an encoder turns one lit input line back into a 2-bit number (a priority encoder picks the highest line when several are on), and a display decoder turns a 4-bit number into the bars of a 7-segment digit. In a memory array the address goes into a decoder whose outputs are the word lines; the one active word line selects the row that is read or written (Harris and Harris, section 5.5).

What a decoder does

  1. A decoder reads a binary number and turns on exactly one of its output lines. Two input bits, A1 and A0, count from 0 to 3, so a 2-to-4 decoder has four outputs, Y0 to Y3.

Inside: NOT and AND

Decoders at work

Encoders

Inside the step

;; step 1/9 · decoder-idea
;; about: Inputs A1 A0, Outputs Y0-Y3
2 inputs → 2² = 4 outputs

A1 A0   number   line on
 0  0      0       Y0
 0  1      1       Y1
 1  0      2       Y2
 1  1      3       Y3

A1 is the 2s bit, A0 the 1s bit.

A real part: Texas Instruments' SN74HC139 holds two 2-to-4 decoders. Its outputs are active low: the chosen line goes to 0 and the others stay at 1, the same idea with the levels flipped. In Python a decoder is [int(n == i) for i in range(4)], an encoder is y.index(1), and a priority encoder is max(i for i in range(4) if x[i]).