digiwleeaTools
LearnGlossaryAboutCheat sheetHow a CPU worksToolsOpen the lab →

How to build a 2-to-4 decoder from logic gates

A 2-to-4 decoder turns a 2-bit address into exactly one active output line, and is built from two NOT gates and four AND gates.

A decoder is how a binary number selects one thing: one memory row, one register, one device. The 2-bit input A1 A0 picks which of the four outputs Y0..Y3 goes high; the other three stay low. Every RAM, register file, and instruction decoder contains one.

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 →

What you need

Step by step

  1. Place two inputs: the address bits A1 (the twos place) and A0 (the ones place).
  2. Invert each address bit with a NOT gate, giving NOT A1 and NOT A0.
  3. Wire NOT A1 and NOT A0 into the first AND: its output Y0 fires for address 00.
  4. Wire NOT A1 and A0 into the second AND (Y1, address 01); A1 and NOT A0 into the third (Y2, address 10); A1 and A0 into the fourth (Y3, address 11).
  5. Probe all four outputs. Step the address through 00, 01, 10, 11 and watch exactly one output line follow it.

2-to-4 decoder truth table

A1A0Y0Y1Y2Y3
001000
010100
100010
110001

Exactly one output is 1 in every row: that is the decoder's one-hot guarantee. Scale the same pattern up (3 inverters and 8 ANDs gives 3-to-8) and you have the row selector inside a RAM.

You can also read the full theory in the lesson on decoders. 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.

Build more parts from gates

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 and course.

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

Frequently asked

What gates are needed for a 2-to-4 decoder?

Two NOT gates and four AND gates. Each AND matches one address: it combines the true or inverted version of each address bit, so exactly one AND fires per input combination.

What is a decoder used for?

Selecting one of many things with a binary number: the row lines inside RAM, the write-enable of one register in a register file, or turning an opcode into control signals. Whenever an address must activate exactly one line, a decoder does it.

What is the difference between a decoder and a demultiplexer?

The same hardware, used differently. A decoder activates one output line chosen by the address; a demultiplexer additionally routes a data input onto that chosen line (an enable input on the decoder's ANDs turns it into a demux).