Skip to content

Search is only available in production builds. Try building and previewing the site to test it out locally.

Demo 5.3: Decoders, Encoders, Multiplexers and Demultiplexers

An adder calculates. The four parts in this demonstration do not calculate anything at all. They move a signal from where it is to where it is needed, or they change the form a piece of information is written in, and between them they take up more room on a real board than the arithmetic does.

They are also the four parts that get mixed up with one another more than any others in this module, and the reason is worth stating at the outset. A decoder and a demultiplexer are the same circuit. Not similar, not related: the same gates, the same truth table, the same part number on the same data sheet. The only difference is which pin you decide to call the data and what you were trying to achieve. Once that has been seen once it stops being confusing, so it is built into the demonstration rather than left as a remark.

The other pairing is less tidy. An encoder is a sort of reverse decoder and a multiplexer is a reverse demultiplexer, but an encoder and a multiplexer have nothing in particular to do with each other, and the demonstration does not pretend otherwise.

A decoder takes a code on a few lines and makes one of many outputs active. An encoder takes one of many active inputs and produces the code that names it. A demultiplexer sends one incoming signal down one of several paths. A multiplexer picks one of several incoming signals and passes it on.

You have met a decoder already, in the seven-segment demonstration. That one was really an exercise in Karnaugh maps and it happened to be called a decoder; this is about what the word means.

Decoder gives you three address lines, an enable, and eight lamps. Set the address and watch the lamp. The gate array is drawn in full because the whole idea is visible in it: each AND gate is wired to a different combination of address lines and their complements, so exactly one of them can ever have all its inputs high.

Encoder starts with a rotary switch, which is the version that behaves. Switch to loose push-buttons and press two at once to see what the circuit actually does when the promise it was relying on is broken.

Demultiplexer and Multiplexer both have a stream of bits actually moving along the wires. Change the address while it is running. The controls let you pause, step one bit at a time, and change the speed, and the strips underneath record what each line received, so you can see the message being cut into pieces and put back together.

Decoders, Encoders, Multiplexers and Demultiplexers

Four parts that route rather than calculate, and the two of them that are the same circuit.

A code in, one lamp lit.

  • Three address lines can spell out eight different codes, and a 3-to-8 decoder turns each one into a single active output.
  • Set the address and watch which lamp lights.
  • Exactly one can be on, and the enable pin can turn all of them off at once.
Address
Enable
101 is 5Y5 is high, the other seven are low
3-to-8 decoderA2A1A0EEnableY0Y1Y2Y3Y4Y5Y6Y7
Why only one can be on
  • Each AND gate is wired to a different combination of the address lines and their complements.
  • Gate 5 takes A2, A1 and A0, so it is the only gate whose four inputs are all high when the address is 101.
  • Change any one address bit and that gate loses an input while exactly one other gate gains its last one.
  • This is called one-of-eight or one-hot output.
What the enable is for
  • Every AND gate takes the enable as a fourth input, so dropping it to 0 forces all eight outputs low at once.
  • Without it there is no way to say none of them, because three address lines always spell out some code.
  • Remember this pin.
  • It is the reason a decoder and a demultiplexer turn out to be the same part.
EA2A1A0Y0Y1Y2Y3Y4Y5Y6Y7
100010000000
100101000000
101000100000
101100010000
110000001000
110100000100
111000000010
111100000001
0xxx00000000
An aside: real decoders usually go low, not high
  • The lamps here light when their output goes high, which is the easy version to think about.
  • Most decoders you can actually buy do the opposite: all outputs sit high and the selected one is pulled low.
  • That is called an active-low output, and it is written with a bar over the signal name or with a bubble on the pin.
  • The reason is electrical rather than logical.
  • The output transistors of these families sink current far better than they source it, so a lamp is wired from the supply, through its resistor, down to the decoder pin, and it lights when the pin pulls that end to ground.
  • The logic is identical and only the polarity is inverted.
  • A 74138 is the 3-to-8 decoder drawn above with active-low outputs and three enable pins rather than one, two of which are themselves active low.
  • Right now the address is 101 and lamp Y5 is the one lit.
  • Seven AND gates have at least one low input; that one has none.

Open Decoder and set the three address bits to 101. Lamp Y5 lights and the other seven stay dark.

The dashed outline is the part. Everything inside it is one component with eleven pins, not eight AND gates and three inverters that you would buy separately, and the address lines and outputs cross that boundary as pins do.

Now find gate 5 in the drawing and follow its four inputs back to the buses they come from. It takes A2, the complement of A1, A0, and the enable. Those four are all high only when the address is 101, and no other gate is wired to that combination. Change one address bit and gate 5 immediately loses an input while exactly one other gate gains its last one.

That is what one-of-eight means, and it is not a rule imposed on the circuit from outside. It follows from the wiring: eight AND gates, eight different combinations, and only one combination present at a time.

Drop the enable to 0. Every lamp goes out, whatever the address says.

This pin is worth more attention than it looks. Three address lines always spell out some code, so without an enable there is no way to say none of them. It is also what lets several decoders share a set of address lines with only one of them responding, which is how a memory bus picks a chip.

Read the aside at the bottom of that tab before moving on. The lamps here light on a high output, which is the easy version to picture, but most real decoders do the opposite, and you will meet that convention constantly.

Open Encoder. Turn the rotary switch and read the three-bit code. Eight positions need three bits because three bits is what it takes to number eight things.

The dashed outline matters here more than anywhere else in this demonstration. Three OR gates that never touch one another look like three unrelated circuits; they are one part, with eight pins coming in and three going out, and the outline is what says so.

Each input enters once, on the left, and the conductor behind it branches inside the part to reach every gate that needs it. The junction dots are where those branches are taken, and reading them down a gate rather than across an input gives you the binary table on its side.

The circuit is remarkably small: three OR gates, and no gate at all for input D0, since the code for zero is three zeros and nothing has to happen to produce it. Output bit 0 is the OR of the four inputs whose number is odd. Bit 1 is the OR of the four with a 2 in them. Bit 2 is the OR of the top four. That is the entire encoder.

Switch to Eight separate buttons and press D1 and D2 together.

The output reads 011, which is the code for D3, an input nobody touched. Nothing has gone wrong with the gates. They were always going to merge whatever arrived, and the rotary switch was quietly guaranteeing that only one thing ever did. The circuit was never correct; it was relying on a promise that the mechanism happened to keep.

Be clear about whose promise it is. A plain encoder specifies one active input and contains nothing that enforces it, so guaranteeing that condition is part of your design work rather than part of the part you bought. The rotary switch discharges the duty mechanically, because one wiper cannot touch two contacts, and that is why the encoder behaved on the previous screen. Eight independent buttons discharge nothing. Wire them straight to an encoder and you have built something that is correct only for as long as your users behave, which is not a property you can put in a specification.

Then release everything and look at the output. It reads 000, which is also what it reads when D0 alone is pressed. A plain encoder cannot tell those two situations apart either.

Both problems are fixed the same way, and the panel beside it shows what a priority encoder would have said: the highest active input, and a separate valid output that goes low when nothing is active at all. This is the part an interrupt controller uses when several devices ask for attention in the same instant.

Step 5: Send a stream down one of four tracks

Section titled “Step 5: Send a stream down one of four tracks”

Open Demultiplexer. A stream of bits is arriving on the single data line and being sent down whichever of the four outputs the address names. Think of it as a set of points on a railway: the train has no say in the matter, and the points decide.

The dashed outline again marks the part. The address lines are inside it because they belong to the device; the data wires cross it because they are what you connect to it. Notice that a bit is grey on the way in and takes the colour of its branch only once it has been through the demultiplexer, because until then it has no destination.

Now change the address while it is running, and watch the bits that are already past the junction. They carry on down the old branch, because the address is read at the moment a bit arrives at the junction and not before. The four received strips at the bottom show the result: the message has been cut into pieces and each piece filed under whichever address was set when it went through.

Use Pause and Step if it moves too quickly. Stepping one bit at a time is the clearest way to see that only one output is ever live.

Scroll to the panel at the bottom of that tab.

That drawing is the decoder from Step 1, at half the width, with exactly one thing changed: the pin labelled Enable is now labelled D and the data goes in through it. Follow the logic. When D is 1, the addressed AND gate goes high and the rest stay low, which is decoding. When D is 0, every output is low. So the addressed output copies D and the others do not, which is demultiplexing.

The circuit cannot tell which of the two jobs it is doing, because there is no difference. This is why parts are sold with both words printed on them.

Open Multiplexer. Four streams arrive, each with its own repeating pattern and its own colour, and the address decides which one is allowed onto the outgoing line.

Change the address a few times while it runs, then read the Y received strip. The colours show exactly where you switched. That splice is the whole idea behind time-division multiplexing: give each source a turn, cycle the address quickly enough, and one wire carries four conversations. The receiver at the far end has to know the same schedule, or it cannot put the pieces back where they belong.

Look at the three unselected inputs while you are there. They are faded because nothing happens to them at all. They arrive, they are not taken, and no record is kept. A multiplexer is a switch and not a queue, and anything that must not be lost has to be held somewhere until its turn comes. Holding something is not a combinational job.

Step 8: Build any function you like out of a multiplexer

Section titled “Step 8: Build any function you like out of a multiplexer”

The last panel on that tab is a small piece of engineering worth having in your head.

Take an 8-to-1 multiplexer, wire its three address lines to A, B and C, and tie each of its eight data inputs to a fixed 0 or 1. The output is now whichever constant the address happens to select, which means those eight constants are the truth table of a three-variable function. Set them and read off what you have built.

Any of the 256 functions of three variables is one wiring away, with no gates at all. This is a third answer to the universality question from earlier in the module, alongside NAND and NOR, and it is also how a lookup table inside an FPGA works.

Quiz
Select 0/1

What is the difference between a 2-to-4 decoder and a 1-to-4 demultiplexer?

Quiz
Select 0/1

An 8-to-3 encoder is built from three OR gates. Two of its inputs, D1 and D2, become active at the same time. What appears on the output?

Concept Match

Match each part to the job it does

Quiz
Select 0/1

Four sensors send data continuously into a 4-to-1 multiplexer. The address is set to input 2 for a while, then changed to input 0. What happened to the data that arrived on inputs 0, 1 and 3 during that first period?

Quiz
Select 0/1

An 8-to-1 multiplexer has A, B and C on its three address lines, and its eight data inputs are tied to fixed 0s and 1s. What has been built?

Five things to take from this.

  1. Two of these parts go from few lines to many and two go from many to few. Within each direction, one is about naming and the other is about routing.
  2. A decoder and a demultiplexer are the same circuit. The enable pin becomes the data pin and nothing else changes.
  3. An encoder is correct only while exactly one input is active, and nothing inside it enforces that. A priority encoder adds the logic that makes the guarantee its own, and a valid output to distinguish nothing from input zero.
  4. Multiplexing and demultiplexing are a matched pair. One wire can carry several conversations if both ends agree on whose turn it is.
  5. A multiplexer with constants on its data pins is a function generator, and a lookup table in an FPGA is exactly that idea at scale.

There is one thing none of these parts can do, and it came up twice. The multiplexer throws away the inputs it was not watching, and a demultiplexed message is only reassembled correctly if something at the far end knows what came before. Both of those need a circuit that can hold a value after the input that produced it has gone away.

Every circuit in the module so far settles to an answer determined entirely by what its inputs are doing at this instant. That is what combinational means, and it is where this half of the subject ends. Next comes logic that remembers.