4-to-2 encoder
An encoder is a combinational circuit that takes 2ⁿ input lines, of which one is active, and outputs the n-bit binary number of the active line.
Turns one hot line back into a number.
Circuit diagram
Toggle the inputs and follow the signals: green wires are high, red are low. The highlighted row of the truth table below is the one you have set.
Truth table
| I0 | I1 | I2 | I3 | o1 | o0 |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 1 | 1 | 1 |
| 0 | 0 | 1 | 0 | 1 | 0 |
| 0 | 0 | 1 | 1 | 1 | 1 |
| 0 | 1 | 0 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 | 1 | 1 |
| 0 | 1 | 1 | 0 | 1 | 1 |
| 0 | 1 | 1 | 1 | 1 | 1 |
| 1 | 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 | 1 | 1 |
| 1 | 0 | 1 | 0 | 1 | 0 |
| 1 | 0 | 1 | 1 | 1 | 1 |
| 1 | 1 | 0 | 0 | 0 | 1 |
| 1 | 1 | 0 | 1 | 1 | 1 |
| 1 | 1 | 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 1 | 1 |
Boolean expressions
- o1
- c ∨ d the high bit: I2 or I3
- o0
- b ∨ d the low bit: I1 or I3
How it works
The reverse of a decoder: four lines in, a two bit number out saying which one is high. It assumes exactly one input is active. If two are, the output is the OR of their codes rather than either of them, which is why real designs use a priority encoder that picks the highest instead. Note also that all zeros and I0 alone both give 00, so a real encoder adds a valid output to tell them apart.
Where it is used
- Reading a keypad, where one key is down at a time.
- Interrupt handling, usually as a priority encoder so simultaneous requests resolve.
- Compressing a one hot state register back into a binary state number.
Build it
Two OR gates is the whole circuit. Making it a priority encoder, so that a higher line wins when two are high, needs one more NOT and AND here: o0 becomes I3 or (I1 and not I2).
Or draw it from the expressions above with the circuit diagram generator, in either symbol standard, and export it as SVG, PNG, Verilog or VHDL.
Reference card
The diagram above as an image, black on white, for notes or a slide.
Click to download: 4-to-2 encoder circuit diagramQuestions about the 4-to-2 encoder
What is an encoder used for?
Reading a keypad or a bank of switches where one is pressed at a time, compressing a one hot state back into a binary state number, and, as a priority encoder, resolving several simultaneous interrupt requests into the number of the most urgent one.
What is a priority encoder?
An encoder that gives a defined answer when more than one input is active: the highest numbered active input wins. A plain encoder simply ORs the codes of the active inputs together, which gives the wrong number whenever two active inputs have different codes, so real designs almost always add priority and a valid output that says whether any input was active at all.