Skip to content
digiwleeaTools
LearnGlossaryAboutCheat sheetHow a CPU worksBuild a CPUToolsOpen the lab →

4-to-2 encoder: truth table and circuit, live

A 4-to-2 encoder turns one active input out of four, D0 to D3, into its 2-bit binary number Y1 Y0, using two OR gates: Y1 = D2 + D3 and Y0 = D1 + D3. This one adds a valid output V = D0 + D1 + D2 + D3, so you can tell input D0 apart from no input at all.

The encoder is the reverse of a 2-to-4 decoder. Each output bit is the OR of the inputs whose number has that bit set: D2 (10) and D3 (11) have the high bit, D1 (01) and D3 (11) the low bit. D0 encodes as 00, the same as when nothing is pressed, which is what V is for. Here the OR of D2 and D3 does double duty, as Y1 and as half of V, so the whole circuit is four 2-input OR gates. A plain encoder assumes exactly one input is 1. Turn on D1 and D2 together and it outputs 11, a 3 that nobody pressed, because each output just ORs the codes of the active inputs; the live circuit lets you try it. A 4-to-2 priority encoder fixes that by letting the highest-numbered active input win, and the 8-to-3 encoder is the same idea one size up.

Live circuit: click the inputs to toggle them and watch the outputs update. It runs the real simulator, right here on the page.

Open this circuit in the lab →

Embed or cite this circuit

Free to embed on a course page, blog or worksheet: paste this code. The circuit stays live, and the small digiwleea badge links back here.

To cite it: digiwleea, “4-to-2 encoder”, https://digiwleea.wleeaf.dev/tools/build/encoder-4to2/

What you need

Step by step

  1. Place inputs D3 to D0 and probes Y1, Y0 and V.
  2. Y1: OR together D2 and D3, the inputs whose number is 2 or more.
  3. Y0: OR together D1 and D3, the odd-numbered inputs.
  4. V: OR D0 with D1, then OR that with the D2 + D3 gate from Y1.
  5. Turn on one input at a time: Y1 Y0 spell its number and V is 1. With every input off, V is 0.

4-to-2 encoder truth table

D3D2D1D0Y1Y0V
0000000
0001001
0010011
0100101
1000111

These five rows are the ones an encoder is meant for: no input, or exactly one. The other 11 combinations have two or more inputs on, and the outputs are then the OR of their codes, for example D1 and D2 give 01 OR 10 = 11. All 16 rows of the live circuit are checked against Y1 = D2 + D3, Y0 = D1 + D3 and V = D0 + D1 + D2 + D3 before the page is built. Its opposite, the decoder, is compared in encoder vs decoder.

You can also read the full theory in the lesson on encoders and demultiplexers. Prefer to build it yourself from scratch? Open the digiwleea lab and place the gates on the canvas, or start from the logic gate simulator.

More builds: Selecting, routing and decoding

All 246 build guides, by topic

Each of these is one step from a transistor to a working CPU. Build every gate from CMOS transistors and work all the way up in the free digiwleea lab, with the free theory reference alongside.

Open the 4-to-2 encoder in the lab →

Frequently asked

What is a 4 to 2 encoder?

A combinational circuit with 4 inputs and 2 outputs that outputs the 2-bit binary number of its one active input: D0 gives 00, D1 01, D2 10 and D3 11.

What are the equations of a 4 to 2 encoder?

Y1 = D2 + D3 and Y0 = D1 + D3, two OR gates. D0 does not appear, because its code is 00. A valid output, V = D0 + D1 + D2 + D3, tells D0 apart from no input.

What happens if two inputs of a 4 to 2 encoder are 1?

A plain encoder outputs the OR of the two codes, which can be a wrong number: D1 and D2 together give 11, the code for D3. A priority encoder avoids this by encoding only the highest-numbered active input.

What is the difference between a 4 to 2 encoder and a 2 to 4 decoder?

They are inverses. The decoder turns a 2-bit number into one active line out of four; the encoder turns one active line back into its 2-bit number.