A processor cannot give every possible input its own wire into every circuit. It needs a way to choose one of several signals, and a way to turn a number into a single active line (or the other way round). Those jobs belong to three more building blocks: the multiplexer, the encoder and the decoder. In this lesson you will trace each one by hand and in C, and see how a multiplexer can use a counter on its select lines to step through its inputs.
What you will learn
- What a multiplexer (mux) does, and how many select lines an n-to-1 mux needs.
- How to write the Boolean expression and truth table of a 2-to-1 mux, and how to build a 4-to-1 mux from 2-to-1 muxes.
- What a decoder does (n bits to 2n one-hot lines) and what an encoder does (the reverse).
- Where these blocks appear inside a processor: choosing a register, enabling a memory location, building a truth table.
- How to write a mux and a decoder in C.
Part 1: The multiplexer
A multiplexer (mux) is a circuit that chooses one of several data inputs and copies it to a single output Y. The choice is made by selection lines (select inputs). A 2-to-1 mux has two data inputs I0 and I1 and one select line S:
Y = (NOT S · I0) + (S · I1)
When S = 0, Y = I0; when S = 1, Y = I1. In words: the select bit is a switch. A 2-to-1 mux is four rows in a truth table, but you do not need to memorise them if you remember the sentence.
In general a multiplexer with n select lines can choose among 2n data inputs. So a 4-to-1 mux has two select lines S1S0, and an 8-to-1 mux has three. Treat the select bits as a binary number and that number is the index of the input that appears at Y. For a 4-to-1 mux:
| S1 | S0 | Index | Y equals |
| 0 | 0 | 0 | I0 |
| 0 | 1 | 1 | I1 |
| 1 | 0 | 2 | I2 |
| 1 | 1 | 3 | I3 |
You can build a 4-to-1 mux from three 2-to-1 muxes: two of them choose between I0/I1 and I2/I3 using S0, and the third chooses between those two results using S1. The same idea scales up. A multiplexer is also a compact way to implement any truth table: put the desired output column on the data inputs and feed the original variables to the select lines.
Try it 1Multiplexer explorer
Choose a 2-to-1 or 4-to-1 mux. Tap the select bits and the data inputs. The highlighted input is the one that S is pointing at, and Y copies it.
Part 2: Decoders and encoders
A decoder takes an n-bit number and turns it into 2n output lines, of which exactly one is 1 (a one-hot pattern). A 2-to-4 decoder has inputs A1 A0 and outputs Y0 to Y3: when A1 A0 = 10 (the number 2), only Y2 is 1. Decoders are used to enable one memory location or one register out of many, given its address. A 3-to-8 decoder has three address bits and eight output lines, and so on.
An encoder does the reverse: it looks at 2n input lines (normally with exactly one of them 1) and outputs the n-bit number of the line that is high. A 4-to-2 encoder with I2 = 1 output A1 A0 = 10. If several inputs can be 1 at once, a priority encoder is used: it reports the highest-numbered input that is 1. For CAPE, be able to explain the simple (one-hot) case and to name the priority version.
Notice the pairing: a decoder expands a compact number into a one-hot choice; a mux uses a compact number to pick one data bit; an encoder packs a one-hot choice back into a number. They often appear together in a processor’s control and in input/output interfaces (a keypad encoder is a common example).
Try it 2Decoder and encoder
In decoder mode, set the address bits A1 A0 and watch the one-hot outputs Y3..Y0. In encoder mode, tap one input I0..I3; the output is that input’s 2-bit number.
Part 3: Muxes, decoders and a counter together
If the select lines of a 4-to-1 mux are driven by the two outputs of the ripple counter in D and JK Flip-Flops, Registers and Counters, then as the counter steps 00, 01, 10, 11 the mux output steps through I0, I1, I2, I3. That is a simple way to turn four stored bits into a stream, one per clock pulse, and it is a pattern CAPE has used in longer Paper 02 questions. The same idea with a decoder would light one of four lamps in turn.
Part 4: These blocks in C
A 2-to-1 mux in C is an if (or an array index). A 4-to-1 mux can be built from three 2-to-1 muxes, just as in Part 1. The data inputs in the second demo are I0..I3 = 0, 1, 1, 0, so the output column should be 0, 1, 1, 0 as S runs from 00 to 11.
#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
A 2-to-4 decoder is a one-hot test against the input number n. Only the matching Y is 1.
#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 this: write a 4-to-2 encoder that looks through I0..I3, finds the (only) 1, and prints its 2-bit index. Test it with each of the four one-hot patterns.
Try it 3Mux, decoder and encoder drill
Selection-line questions use the 2^n rule. For a 4-to-1 mux, treat S1 S0 as a binary number.
Summary
| Block | Key points |
| Multiplexer | Chooses one of 2n data inputs using n select lines. 2-to-1: Y = (NOT S · I0) + (S · I1). |
| 4-to-1 mux | S1 S0 is the index of the input that appears at Y. Can be built from three 2-to-1 muxes. |
| Decoder | n bits in, 2n lines out, one of them 1 (one-hot). Used to enable one address or register. |
| Encoder | The reverse: 2n (usually one-hot) inputs to an n-bit number. A priority encoder breaks ties. |
| Together | A counter on a mux’s select lines steps through the data inputs. A decoder with the same counter lights one line at a time. |
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?
Show answer
S1 S0 = 10 is the index 2, so Y = I2 = 1.
4. A 2-to-4 decoder has A1 A0 = 01. List Y3 Y2 Y1 Y0.
Show answer
The input is the number 1, so Y1 is 1 and the others are 0: 0 0 1 0.
5. What is the difference between a decoder and a demultiplexer?
Show answer
A decoder turns an address into a one-hot enable. A demultiplexer also uses an address, but it routes a single data input out along the chosen line (the others stay 0). They are close cousins; a decoder is a demultiplexer whose data input is tied to 1.
6. A 4-to-2 priority encoder has I3 I2 I1 I0 = 0 1 1 0. What does it output, and why is the word "priority" needed?
Show answer
I2 and I1 are both 1. A priority encoder reports the highest index that is 1, so 2, and A1 A0 = 10. Without priority the output would be undefined.
7. A 2-bit counter feeds the select lines of a 4-to-1 mux whose inputs are 1, 1, 0, 1. What 4-bit sequence appears at Y as the counter runs 00, 01, 10, 11?
Show answer
1, 1, 0, 1: the inputs in order.
Homework
- Draw a clearly labelled 2-to-1 mux (inputs I0, I1, S, output Y) and write its expression and 4-row truth table. Then draw a 4-to-1 mux as a box, and also as three 2-to-1 muxes.
- Complete a 3-to-8 decoder table for inputs 000 to 111 (you only need to mark which Y is 1 in each row). Check three of the rows with the idea of Try it 2.
- Change
mux.cso the 4-to-1 data is 1, 0, 1, 1 and confirm it against Practice question 3. - Read Multiplexers on this site and compare its diagrams with yours.
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", "encoder vs decoder vs multiplexer".
Try it 4Quick quiz
Tap an answer. All eight questions are self-checks on this lesson.
Updates
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.