A computer is full of places where several signals could go down the same wire, but only one of them should go at a time. The circuit that makes that choice is a multiplexer (a mux). The opposite job, spreading one compact code out onto many wires, is done by a decoder, and packing many wires back into a short code is done by an encoder.
In this lesson you will build those three blocks from the AND, OR and NOT gates you already know, watch the live wires change colour as you toggle the inputs, and see how a mux can stand in for a whole truth table. The same rules are then written in C, so you can check a 2-to-1 mux, a 4-to-1 mux and a 2-to-4 decoder on a computer.
What you will learn
- What a multiplexer does, how the select lines choose a data input, and the 2n rule that links the number of select lines to the number of data inputs.
- How to draw a 2-to-1 mux as a labelled block, and how to build it from AND, OR and NOT gates.
- How a 4-to-1 mux is built, including a version made from 2-to-1 muxes, and how to trace Y for a given S1 S0.
- What a decoder and an encoder do, including a priority encoder when two inputs are 1 at once.
- How to implement any 2-input truth table by tying the data pins of a 4-to-1 mux to 0 or 1.
- How a counter wired to the select lines of a mux steps through the data inputs, which is the idea behind some exam questions.
- How the same rules are written as short C functions.
Part 1: The multiplexer
Think of a mux as a digitally controlled switch. It has several data inputs (I0, I1, …), a smaller group of select lines (S, or S1 and S0, and so on), and a single output Y. The select lines form a binary number. That number is the index of the data input that is copied to Y. All the other data inputs are ignored for as long as the select bits stay the same.
The counting rule is simple and worth memorising: n select lines can choose among 2n data inputs. So a 2-to-1 mux needs one select line, a 4-to-1 mux needs two, an 8-to-1 mux needs three, and a 16-to-1 mux needs four. If a question gives you the number of data inputs, take the power of two; if it gives you the number of select lines, raise two to that power.
A labelled 2-to-1 block
Exam questions often ask you to draw a mux as a labelled block rather than as a nest of gates. Draw a trapezium or a rectangle, put I0 and I1 into one side, put S on a third side, put Y out of the opposite side, and name every arrow. Marks are lost more often for a missing label than for a slightly messy shape. Here is the standard 2-to-1 block, with every pin named:
A 2-to-1 multiplexer drawn as a labelled block. I0 is the input chosen when S = 0; I1 is chosen when S = 1.
Worked example 1: the 2-to-1 truth table
For a 2-to-1 mux the Boolean expression is:
Y = (NOT S · I0) + (S · I1)
Read that in words: when S is 0, the first AND gate is enabled (because NOT S is 1) and Y copies I0; when S is 1, the second AND gate is enabled and Y copies I1. The two AND outputs are merged by an OR gate, but only one of them is 1 at a time, because S and NOT S cannot both be 1.
Work through one row slowly. Suppose S = 0, I0 = 1 and I1 = 0.
- NOT S = NOT 0 = 1.
- The top AND computes NOT S · I0 = 1 · 1 = 1.
- The bottom AND computes S · I1 = 0 · 0 = 0.
- The OR of those two results is 1 + 0 = 1, so Y = 1, which is a copy of I0.
Now flip only the select bit: S = 1, I0 = 1, I1 = 0. NOT S is now 0, so the top AND is forced to 0. The bottom AND is 1 · 0 = 0. The OR is 0, so Y = 0, a copy of I1. The data inputs did not change; only the selected path changed. That is the whole point of a mux.
| S | I0 | I1 | Y |
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 1 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 0 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 0 | 0 |
| 1 | 1 | 1 | 1 |
Look down the Y column and check it against the rule: the first four rows, where S = 0, copy the I0 column; the last four rows, where S = 1, copy the I1 column.
A 4-to-1 mux, and two ways to build it
A 4-to-1 mux has data inputs I0, I1, I2 and I3, and two select lines S1 (the more significant bit) and S0 (the less significant bit). Read S1 S0 as a two-bit number: 00 selects I0, 01 selects I1, 10 selects I2 and 11 selects I3. Always treat I0 as the input chosen when the select bits are 00.
One way to build it is with four AND gates and one OR gate. Each AND gate has three inputs: one data bit, and a unique combination of S1, S0, NOT S1 and NOT S0 that is 1 for only one of the four select codes. The four AND outputs go into a four-input OR, and that OR is Y. This is the same idea as a decoder (Part 2) feeding a row of AND gates.
A neater construction uses three 2-to-1 muxes. The first mux, selected by S0, chooses between I0 and I1. The second mux, also selected by S0, chooses between I2 and I3. The third mux, selected by S1, chooses between those two results. You will see exactly this nesting in the C program later: a 4-to-1 mux written as mux2 calls inside mux2. Both constructions give the same Y; the nested version is often easier to draw once you already trust the 2-to-1 block.
Worked example 2: tracing a 4-to-1 mux
Let I0 I1 I2 I3 = 1 0 1 1, and let S1 S0 = 10. First convert the select bits: 10 is the number 2, so the mux must copy I2. I2 is 1, so Y should be 1. Now check it at gate level. NOT S1 = 0 and NOT S0 = 1. The enable line that needs S1 = 1 and S0 = 0 is the enable for I2; that line is 1. The I2 AND gate therefore outputs 1 · 1 = 1. The other three AND gates are disabled, so they output 0. The OR of 0, 0, 1 and 0 is 1. Y is 1, as required.
If the same data is kept and you only change S1 S0 to 01, the index becomes 1, the I1 AND gate opens, I1 is 0, and Y becomes 0. Practise this trace until you no longer have to guess.
Use the live circuit below to confirm both worked examples, then try values of your own. Start in Block view to see which input is routed to Y, then switch to Gate view to trace the AND, OR and NOT gates. Orange wires are at 1; grey wires are at 0, and the selected path is drawn thicker.
Try it 1Multiplexer: block and gate view
Choose Block view or Gate view, pick 2-to-1 or 4-to-1, then tap the select bits and data inputs. Wires light orange when they carry 1, and the selected path is drawn thicker.
Part 2: Decoders and encoders
A decoder takes a compact binary code and turns it into a one-hot pattern: exactly one of its output lines is 1, and the rest are 0. A 2-to-4 decoder has two inputs, A1 and A0, and four outputs Y0, Y1, Y2 and Y3. When A1 A0 = 00, only Y0 is 1; when A1 A0 = 10, only Y2 is 1, and so on. The input is being read as a number, and the output line with that number is raised.
This is how a processor can enable one memory location, or one register, given its address. The address is a short binary number; the decoder raises a single enable line for the chosen cell and leaves the others off, so only that cell is written or read.
Inside, a 2-to-4 decoder is four AND gates and two NOT gates. Each AND gate gets a unique mix of A1, A0, NOT A1 and NOT A0, just as each AND gate of a 4-to-1 mux got a unique mix of the select bits. In fact, the enable lines of a 4-to-1 mux are a decoder. Once you see that, the two circuits start to look like parts of the same idea.
Worked example 3: a 2-to-4 decoder
Let A1 A0 = 10. That is the number 2, so Y2 must be 1 and Y0, Y1 and Y3 must be 0. At the gates: NOT A1 = 0, NOT A0 = 1. The AND gate for Y2 is the one that wants A1 = 1 and A0 = 0; those values are present, so Y2 = 1. The AND gate for Y0 wants NOT A1 and NOT A0; NOT A1 is 0, so Y0 = 0. Y1 wants NOT A1 and A0: NOT A1 is again 0, so Y1 = 0. Y3 wants A1 and A0: A0 is 0, so Y3 = 0. Only one line is high.
An encoder does the reverse. A 4-to-2 encoder looks at four one-hot inputs I0 to I3 and writes the binary number of the line that is 1. If I2 is 1 and the others are 0, the output A1 A0 is 10. You can build A1 with an OR of I2 and I3 (the inputs whose number has a 1 in the 2s place) and A0 with an OR of I1 and I3 (the odd numbers).
A plain encoder is only well-defined when exactly one input is 1. If two inputs are 1 at once, the OR gates mix their codes and the output is no longer a clean number. A priority encoder solves this by picking the highest-numbered input that is 1 and ignoring the others. If I1 and I3 are both 1, a 4-to-2 priority encoder outputs 11 (the code for I3), not 01. Priority encoders turn up in processors when more than one device asks for attention at the same time: the one with the higher priority wins.
Worked example 4: encoder and priority encoder
First the ordinary case. I0 I1 I2 I3 = 0 0 1 0, so only I2 is high. I2 is the number 2, which is 10 in binary, so A1 = 1 and A0 = 0. Check the OR gates: A1 = I2 OR I3 = 1 OR 0 = 1; A0 = I1 OR I3 = 0 OR 0 = 0. Agreed.
Now let I0 I1 I2 I3 = 0 1 0 1, so I1 and I3 are both 1. A plain encoder would compute A1 = 0 OR 1 = 1 and A0 = 1 OR 1 = 1, which happens to be the code for I3, but only because I3 includes both bits. If the two highs had been I1 and I2, a plain encoder would output A1 = 1 and A0 = 1, which is not the code of either input — the result is meaningless. A priority encoder looks from I3 downwards, finds I3 already at 1, and outputs 11, ignoring I1. That is a defined, repeatable answer, which is why exam questions that allow more than one active input are almost always about priority encoders.
Use the live circuit to try all three modes. In decoder mode, tap A1 and A0 and watch a single Y line light up. In encoder mode, tap one I input at a time (the widget keeps the inputs one-hot, which is the honest way to use a plain encoder). In priority mode you can raise several inputs at once and see the highest one win.
Try it 2Decoder, encoder and priority encoder
Switch between Block view and Gate view, choose Decoder, Encoder or Priority encoder, then tap the inputs. Orange wires carry 1.
Part 3: A mux as a truth-table builder, and a mux driven by a counter
Here is a trick that makes muxes even more useful. Tie each data input of a 4-to-1 mux to a constant 0 or a constant 1, and treat the two select lines as the variables A and B of a 2-input function. The four data pins are then exactly the four rows of a truth table, in the order 00, 01, 10, 11. Whatever pattern you tie onto I0 I1 I2 I3 appears, row by row, as Y.
That means you can implement any 2-input Boolean function with one 4-to-1 mux and a handful of fixed 0s and 1s, without building a custom nest of gates for each function.
Worked example 5: XOR from a 4-to-1 mux
XOR is 1 when its two inputs are different. Its truth table for AB = 00, 01, 10, 11 is 0, 1, 1, 0. Tie I0 = 0, I1 = 1, I2 = 1 and I3 = 0, and let S1 be A and S0 be B. When AB = 10, the mux selects I2, which is 1, and Y is 1, which is what XOR should give for 10. When AB = 00, it selects I0 = 0; when AB = 11, it selects I3 = 0. The four pins are the XOR column. NAND would be 1, 1, 1, 0; AND would be 0, 0, 0, 1. Try those patterns in the widget below and watch the truth table fill in.
Worked example 6: a counter on the select lines
In D and JK Flip-Flops, Registers and Counters you built a 2-bit ripple counter that counts 00, 01, 10, 11 and then wraps. Wire that counter to the select lines of a 4-to-1 mux, and as the count ticks, Y will step through I0, I1, I2 and I3 in order. If the data pins hold 1 0 1 0, the output Y is the repeating pattern 1, 0, 1, 0 — a simple way to generate a chosen sequence from a counter. Later exam questions sometimes combine a mux with a counter in exactly this way: first write down the count at each step, then read off Y from the data pin that the count selects.
In the builder below, tap a data pin to tie it to 0 or 1, and tap a row (or a named preset) to choose the current select code. The selected path through the mux block lights up, and the truth table highlights the matching row. If the pin pattern is a named gate such as XOR or NAND, the caption will say so.
Try it 3Mux as a truth-table builder
Tie each data pin of a 4-to-1 mux to 0 or 1. The two select bits are the inputs A and B of a 2-input function. Tap a row to choose S1 S0, or start from a named gate. The selected path through the mux lights up, and the truth table highlights that row.
Part 4: These blocks in C
Hardware and C are saying the same thing, just in a different language. A 2-to-1 mux is an if on the select bit; a 4-to-1 mux can be written as two such choices nested together, which is the same construction as the three 2-to-1 muxes on paper. The decoder loop below raises exactly one of four variables, the way the live circuit raises exactly one Y line. Run the programs, then change a data pin or an address and compile them again, to confirm the traces you have already done by hand.
The first program prints a 2-to-1 mux for one pair of inputs, then a 4-to-1 mux whose data is 0, 1, 1, 0, for every value of S1 S0. Check the 4-to-1 table against the 2n index rule: when S1 S0 is 10 the index is 2, so Y should be the third data bit, which is 1.
#include <stdio.h>
/* A 2-to-1 multiplexer, then a 4-to-1
built by selecting with two bits. */
int mux2(int s, int i0, int i1)
{
if (s == 0)
return i0;
return i1;
}
int main(void)
{
int s1, s0, i[4] = {0, 1, 1, 0};
int y;
printf("2-to-1: S I0 I1 | Y\n");
printf(" 0 0 1 | %d\n",
mux2(0, 0, 1));
printf(" 1 0 1 | %d\n",
mux2(1, 0, 1));
printf("4-to-1, I = 0 1 1 0\n");
printf("S1 S0 | Y\n");
for (s1 = 0; s1 <= 1; s1++)
for (s0 = 0; s0 <= 1; s0++) {
y = mux2(s1,
mux2(s0, i[0], i[1]),
mux2(s0, i[2], i[3]));
printf(" %d %d | %d\n",
s1, s0, y);
}
return 0;
}
Output:
2-to-1: S I0 I1 | Y
0 0 1 | 0
1 0 1 | 1
4-to-1, I = 0 1 1 0
S1 S0 | Y
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 0
The second program is a 2-to-4 decoder. For each address 0 to 3 it writes A1 A0 and then the four outputs Y3 Y2 Y1 Y0, with a single 1 in the column whose number is the address. (The printout lists Y3 first, which is a common way to write a one-hot pattern from the high line down to Y0.)
#include <stdio.h>
/* A 2-to-4 decoder: one of four lines
is 1, chosen by A1 A0. */
int main(void)
{
int a1, a0, n, y0, y1, y2, y3;
printf("A1 A0 | Y3 Y2 Y1 Y0\n");
for (n = 0; n < 4; n++) {
a1 = n / 2;
a0 = n % 2;
y0 = (n == 0);
y1 = (n == 1);
y2 = (n == 2);
y3 = (n == 3);
printf(" %d %d |", a1, a0);
printf(" %d %d", y3, y2);
printf(" %d %d\n", y1, y0);
}
return 0;
}
Output:
A1 A0 | Y3 Y2 Y1 Y0 0 0 | 0 0 0 1 0 1 | 0 0 1 0 1 0 | 0 1 0 0 1 1 | 1 0 0 0
Try it 4Mux, decoder and encoder drill
Selection-line questions use the 2n rule. For a 4-to-1 mux, treat S1 S0 as a binary number. Encoder questions assume exactly one input is high.
Summary
- A multiplexer copies one of several data inputs to a single output. The select lines form the index of the chosen input. n select lines choose among 2n data inputs.
- A 2-to-1 mux is Y = (NOT S · I0) + (S · I1). A 4-to-1 mux can be built from four AND gates and an OR, or from three 2-to-1 muxes.
- A decoder turns a compact address into a one-hot set of enable lines. An encoder does the reverse. A priority encoder picks the highest active input when more than one is 1.
- Tying the data pins of a 4-to-1 mux to 0 or 1 implements any 2-input truth table. Wiring a counter to the select lines steps Y through those pins in order.
- The same rules can be written in C and checked against a hand trace.
Practice questions
1. Write the Boolean expression for a 2-to-1 mux and evaluate it for S = 1, I0 = 0, I1 = 1.
Show answer
Y = (NOT S · I0) + (S · I1) = (0 · 0) + (1 · 1) = 1.
2. How many select lines does an 8-to-1 multiplexer need? How many data inputs does a mux with 4 select lines have?
Show answer
8 = 23, so 3 select lines. 4 select lines choose among 24 = 16 data inputs.
3. A 4-to-1 mux has I0 I1 I2 I3 = 1 0 1 1 and S1 S0 = 10. What is Y? Trace the enable lines as well as the index.
Show answer
S1 S0 = 10 is the index 2, so Y = I2 = 1. The enable for I2 (S1 = 1 and S0 = 0) is the only enable that is high, so only that AND gate can pass a 1 to the OR.
4. A 2-to-4 decoder has inputs A1 A0 = 01. Which output is 1, and why are the others 0?
Show answer
01 is the number 1, so Y1 is 1. Each other AND gate needs a different mix of A1, A0 and their complements, so Y0, Y2 and Y3 stay 0.
5. A 4-to-2 encoder has I1 = 1 and the other inputs 0. What is A1 A0? How is each bit made from OR gates?
Show answer
I1 is the number 1, so A1 A0 = 01. A1 is I2 OR I3 = 0. A0 is I1 OR I3 = 1.
6. I1 and I3 are both 1 on a 4-to-2 priority encoder. What is the output, and why would a plain encoder be a poor choice if the highs were I1 and I2 instead?
Show answer
The highest 1 is I3, so the priority encoder outputs 11. A plain encoder with I1 and I2 both high would OR the bits together to give 11, which is not the code of either input, so the answer would be meaningless.
7. Show how to make XOR with a 4-to-1 mux. What values do you tie to I0 I1 I2 I3, and which select bits are the variables A and B?
Show answer
Tie I0 I1 I2 I3 = 0 1 1 0. Let S1 be A and S0 be B. Those four pins are the XOR column for AB = 00, 01, 10, 11.
8. A 2-bit counter, starting at 00, is wired to the select lines of a 4-to-1 mux whose data pins are 1 1 0 1. List Y for four successive counts, then the next count after it wraps.
Show answer
Counts 00, 01, 10, 11, 00 give Y = I0, I1, I2, I3, I0 = 1, 1, 0, 1, and then 1 again.
Homework
- On paper, draw a 2-to-1 mux as a labelled block (every pin named) and also as an AND–OR–NOT circuit. Pick values for S, I0 and I1 and write 1 or 0 on every wire.
- Build a 4-to-1 mux from three 2-to-1 muxes on paper. Choose S1 S0 = 10 and I0 I1 I2 I3 = 0 1 1 0, and write the output of each of the three muxes.
- Change
mux.cso the 4-to-1 data is 1, 0, 1, 1 and confirm the printed table against Practice question 3. - Using the truth-table builder, find a pin pattern that implements NOR, write it down, and explain in one sentence why I0 is 1 and the other three pins are 0.
Mastery quiz: Multiplexers, Encoders and Decoders
This 10-question quiz is separate from the short Quick quiz at the end of the lesson. Score 8/10 or higher to earn this lesson’s badge. Your best score is saved in this browser on this device.
Mastery quiz: Multiplexers, Encoders and Decoders
Your course badges: loading saved progress…
View badges
No course badges earned yet. Score 8/10 in a lesson quiz to earn its badge.
Saved in this browser on this device. Clearing browser data may remove your badges.
Answer all 10 questions. Earn this lesson’s badge at 8/10 or higher. Read the explanations, revisit the Try it activities and retry as often as you need. These are self-study badges, not formal grades.
Exam tips
Specimen Unit 1 Paper 02 (2021), Question 1(a). Asks you to draw a 2-to-1 multiplexer as a labelled block, with every input and the output named. Draw the two data inputs, the select line and the output, and name each one. Marks are lost for a missing label more often than for a messy box.
2009 Unit 1 Paper 02, Question 1. Includes a 4-to-1 multiplexer as part of a longer question on the building blocks. Know how S1 S0 pick the input. Worked solution: 2009 U1 Q1.
2024 Unit 1 Paper 02, Question 2. Later parts combine a multiplexer with a counter on the select lines, which is exactly Part 3 of this lesson. Trace the count first, then read off Y at each step. Worked solution: 2024 U1 Q2.
Paper 01 also uses short items of the form "how many select lines does this mux have?" Use the 2n rule and you are done. General advice: always label I0 as the input chosen when the select bits are 00, and always write the select bits as a binary number before you pick an input. More worked answers are on the CS Solutions page.
Previous: D and JK Flip-Flops, Registers and Counters. Next: The Processor: CPU Components and the Fetch-Decode-Execute Cycle. See also: Multiplexers and Logic Gates and Truth Tables.
Syllabus link: CAPE Computer Science (CXC A19/U2/21), Unit 1, Module 1: Computer Architecture. Specific Objective 2: describe the basic building blocks of a computer (multiplexors, encoders, decoders).
Video search terms
Search YouTube for: "2 to 1 multiplexer explained", "4 to 1 mux from 2 to 1", "2 to 4 decoder truth table", "priority encoder explained", "encoder vs decoder vs multiplexer".
Try it 5Quick quiz
Tap an answer. All eight questions are self-checks on this lesson.
Updates
2026/11/24
- Added a block view and a cleaner gate view to the circuit Try its.
- Tidied the gate labels in the circuit Try its.
- Rebuilt the lesson with fuller explanations, diagrams and new Try it simulations.
2026/11/23
- Removed the Print this lesson button.
- Reworded some sentences so they speak directly to students.
- Published.
© 2026 Vedesh Kungebeharry. All rights reserved.


