LogicGates.org Open the simulatorSimulator

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.

o1 0 o0 0

Truth table

I0I1I2I3 o1o0
0000 00
0001 11
0010 10
0011 11
0100 01
0101 11
0110 11
0111 11
1000 00
1001 11
1010 10
1011 11
1100 01
1101 11
1110 11
1111 11

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).

Open the simulator

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.

4-to-2 encoder logic circuit diagram with inputs a = I0, b = I1, c = I2, d = I3: o1 from c ∨ d, o0 from b ∨ d Click to download: 4-to-2 encoder circuit diagram

Questions 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.

The other circuits