Computer Architecture
How a binary decoder works
A binary decoder turns a binary code into exactly one active line. A 2-to-4 decoder takes two input bits and has four outputs, numbered 0 to 3; the code on the inputs picks which one is 1, and the other three stay 0. That's called one-hot. Processors use decoders to turn each instruction's opcode into the one action it names.
Four ANDs and two NOTs
Each output line is an AND of the input bits, with a NOT on every bit that should be 0 for that line:
| Code | Line | Its AND |
|---|---|---|
| 00 | 0 | NOT B1 · NOT B0 |
| 01 | 1 | NOT B1 · B0 |
| 10 | 2 | B1 · NOT B0 |
| 11 | 3 | B1 · B0 |
Only one AND can be 1 at a time, because only one pattern of the two bits can be on the inputs at once. Two NOT gates feed all four ANDs: one output can drive any number of inputs.
Decoding instructions
Give a tiny processor four instructions, each a 2-bit opcode: 00 LOAD, 01 ADD, 10 JUMP, 11 HALT. When an instruction is fetched, its opcode goes into the decoder, and exactly one line wakes: LOAD's line tells the accumulator to load, ADD's tells it to add, and so on. Because the lines are one-hot, two parts of the processor can never act on the same instruction by mistake.
Live experiment
A processor's smallest program
Four numbers in memory, each a 2-bit opcode: 00 LOAD, 01 ADD, 10 JUMP, 11 HALT. Step through and the decoder, two NOTs and four ANDs, lights exactly one line for each. Tap a number to change the program.
“Decode the opcode” is lesson 3 of 6 in Register Reef, Computer Architecture World 1. Free with an account. Lessons unlock in order, so you’ll do the 2 before it first.
More bits, more instructions. An n-bit code has 2ⁿ patterns, so a 3-bit opcode can name 8 instructions, and n inputs need 2ⁿ ANDs, each with n inputs. Real processors use wider opcodes and decode them in stages, but the idea is the same.
Decoding addresses
The same circuit picks memory. A computer's address lines go into a decoder, and each output enables one memory chip or one device, its chip select. The 74HC138, a 3-to-8 decoder, does this job on countless boards: three address bits choose which of eight chips answers. Inside every memory chip, decoders pick one row out of thousands from the address bits.
Decoder vs multiplexer
A decoder turns a code into one line. A multiplexer turns a code into a choice: each decoder line opens a gate on one data input, and an OR joins them, so the code picks which input reaches the output. That's exactly how an ALU picks which of its results to pass on.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“Decode the opcode” is lesson 3 of 6 in Register Reef, Computer Architecture World 1. Free with an account. Lessons unlock in order, so you’ll do the 2 before it first.
Or start from the very first lesson, no account neededPractice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: JUMP. 00 LOAD, 01 ADD, 10 JUMP, 11 HALT. 10 is JUMP, from the AND that takes OP1 and NOT OP0.
- Why not “HALT”? HALT is 11, with both bits set. 10 has only the top bit.
- Why not “ADD”? ADD is 01: the low bit set and the top bit clear, the reverse of 10.
- Why not “LOAD”? LOAD is 00. Opcode 10 has its top bit set, so it can't be LOAD.
Show the answer
Answer: NOT OP1 and NOT OP0. LOAD is opcode 00: both bits must be 0, so both go through a NOT first.
- Why not “OP1 and OP0”? OP1 and OP0 straight is the AND for 11, HALT. LOAD needs both bits to be 0.
- Why not “NOT OP1 and OP0”? NOT OP1 and OP0 is 1 for opcode 01, ADD.
- Why not “OP1 and NOT OP0”? OP1 and NOT OP0 is 1 for opcode 10, JUMP.
Show the answer
Answer: All four lines. Every AND sees 1 and 1. Without NOTs the ANDs can't tell the opcodes apart.
- Why not “LOAD only”? LOAD's line should need both bits at 0. Without NOTs its AND sees 1 and 1 like the rest.
- Why not “Nothing”? With both bits at 1, every AND of the raw bits gives 1, so every line lights, not none.
- Why not “HALT only”? HALT's line should be the only one, but without NOTs every AND sees 1 and 1, so all four light.
“Decode the opcode” is lesson 3 of 6 in Register Reef, Computer Architecture World 1. Free with an account. Lessons unlock in order, so you’ll do the 2 before it first.
Questions people ask
What does a decoder do in digital logic?
It takes an n-bit binary code and turns on exactly one of its 2ⁿ outputs, the one numbered by the code. A 2-to-4 decoder has two inputs and four outputs; a 3-to-8 decoder has three and eight.
What is the difference between a decoder and a multiplexer?
A decoder turns a code into one active line out of many. A multiplexer uses a code to choose one of many data inputs and pass it to a single output. Inside, a multiplexer is a decoder whose lines open gates on the data.
What is a 74HC138?
A common 3-to-8 decoder chip. Its three address inputs pick which of eight outputs goes low, and three enable inputs let several be combined into bigger decoders. It's often used to select one memory or device chip at a time.
Related topics
- What is an ALU, and how does it work?Work out every answer at once, then let the opcode pick one: the heart of a CPU.
- The fetch-decode-execute cycle explainedFetch an instruction, decode it, carry it out, repeat: how every CPU runs.
- What does the program counter do in a CPU?The register that says which instruction comes next, and how jumps make loops.
- Logic gates explainedAND, OR, NOT, XOR and their bubbled twins: each one's rule is a truth table.