Overview
Readings Brown & Vranesic §6.1–6.5 · week 6
Some combinational circuits show up so often that they’re sold as ready-made chips (or used as blocks in CAD tools) so you don’t have to build them from gates every time. This chapter covers comparators, decoders (including the BCD-to-7-segment decoder), encoders, multiplexers, demultiplexers, and parity generators/checkers.
Notes
Comparators
A comparator tells you how two binary numbers relate.
Equality. An XNOR gate is a 1-bit comparator, since its output is 1 when the two bits are equal. To compare two multi-bit numbers, XNOR each pair of matching bits and AND the results. The numbers are only equal if every bit pair matches.
Figure 6.1: A 2-bit equality comparator.
Magnitude (inequality). A magnitude comparator also has and outputs. To decide, start at the most significant bit:
- and → .
- and → .
- → no decision yet, so move to the next bit down and repeat.
The first bit position where the numbers differ decides the answer. If no position differs, . Starting at the MSB works because a 1 in a higher position is worth more than all the lower bits combined (for example ), so nothing below the first difference can change the result. It’s the same way you’d compare 4,821 and 4,793 by eye: scan from the left and stop at the first digit that differs.
Example 6.1: Comparing two numbers
(a) Is equal to using a 2-bit equality comparator? (b) Compare and with a magnitude comparator.
Show answerHide answer
(a) Bit 1: . Bit 0: . AND: , so .
(b) Start at the MSB:
- : no decision
- : no decision
- , : (bit 0 doesn’t matter)
Check: ✓
Decoders
A decoder detects a specific bit combination (code) on its inputs and signals it with an output level. In general it has inputs and up to outputs.
Decoding one code. Think of it as a combination lock that only opens for one pattern. An AND gate is the basic decoding element because it’s 1 only when every input is 1. You invert the inputs that should be 0, so that a 0 in the right place turns into a 1 at the gate. For example, to detect :
For an active-LOW output, use a NAND gate instead.
Example 6.2: Decoding one code
Write the decoding expression for the code ( is the MSB), with an active-HIGH output and with an active-LOW output.
Show answerHide answer
- Invert the inputs that should be 0 (only here):
- Active-LOW: use a NAND gate instead:
The output is 1 (or 0 for active-LOW) only for .
Labeling: an -bit code uses bits down to (a 4-bit code has no ), and a decoder's outputs are named to . So input activates , which is the 7th output if you count from 1.
Decoding every code. A decoder with one output per input combination is called an -line-to--line decoder, or a 1-of- decoder since exactly one output is active at a time.
- 4-line-to-16-line (1-of-16): 4 inputs, 16 outputs, and one AND (or NAND) gate per output.
- BCD-to-decimal (4-line-to-10-line, 1-of-10): only the 10 valid BCD codes get an output.
- 2-line-to-4-line: with inputs , the outputs are , , , . Each output is one minterm.
Example 6.3: Which output of a 3-to-8 decoder fires?
A 3-line-to-8-line decoder has inputs . Which output is active, and what gate produces it?
Show answerHide answer
The input is the binary number , so output is the active one and the other seven are inactive. Output is the minterm
which is a 3-input AND with inverted. The whole decoder needs 8 such ANDs (one per minterm), sharing three inverters. Exactly one output is active for every input combination.
BCD-to-7-segment decoder. This takes a BCD digit and lights the right segments (a–g) of a 7-segment display to show it. Each segment is its own output function of the 4 BCD inputs, simplified with a K-map using the six invalid codes as don’t cares (Ch 4). For segment , which is lit for digits 0, 2, 3, 5, 6, 7, 8, 9:
Try it: set a BCD code and see which segments light up. Each segment’s expression is listed underneath.
Example 6.4: A 7-segment segment equation
Check that lights segment for digit 5 but not for digit 4.
Show answerHide answer
- Digit 5 : , , → , lit ✓
- Digit 4 : , , , → , off ✓ (a “4” has no top bar)
Encoders
An encoder is the reverse of a decoder. It takes an active level on one of its inputs (a digit or symbol) and outputs the matching code.
Decimal-to-BCD encoder (10-line-to-4-line): ten inputs, one per decimal digit, and four BCD outputs. Each output bit is the OR of the digits whose BCD code has a 1 in that position:
| Output bit | Weight | 1 for digits… | Expression |
|---|---|---|---|
| 8 | 8, 9 | ||
| 4 | 4, 5, 6, 7 | ||
| 2 | 2, 3, 6, 7 | ||
| 1 | 1, 3, 5, 7, 9 |
(Here “8 + 9” means the OR of input lines 8 and 9.) Input 0 doesn’t need a gate, since all the outputs are 0 when nothing else is active.
Reading the table by columns. If you forget the expressions, rebuild them. Write each digit 0–9 next to its BCD code. Each output bit is one column of that table, and its OR gate collects every digit that has a 1 in that column. For example, the 1s in the weight-1 column are at 1, 3, 5, 7, 9, giving . That’s also why has five terms and only two: among digits 0–9, every odd digit has a 1 in the last place, but only 8 and 9 have one in the weight-8 place.
Only one input line is HIGH at a time, and the four outputs together form one 4-bit code naming that line (10 lines compressed into 4 bits, since ).
Example 6.5: Decimal-to-BCD encoder
Input line 6 of a decimal-to-BCD encoder is HIGH (all others LOW). What are the outputs?
Show answerHide answer
- Check which output expressions include input 6. and do, and and don’t.
- Outputs: , which is 6 in BCD ✓
Multiplexers (data selectors)
A multiplexer (MUX) routes one of several data inputs onto a single output line, and the data-select inputs choose which one. With select lines it can choose among data inputs.
4-to-1 MUX: data inputs –, select inputs :
| 0 | 0 | |
| 0 | 1 | |
| 1 | 0 | |
| 1 | 1 |
That’s four 3-input ANDs, a 4-input OR, and two inverters for and . Each AND gate is enabled by exactly one select combination (it’s a decoder built into the MUX). The 2-to-1 MUX from Ch 2, , is the smallest case.
Try it: set the data inputs and the select lines, and the selected path lights up through the MUX.
Example 6.6: Multiplexer output waveform
A 4-to-1 MUX has , , , held constant. The select lines step through . What is in each interval?
Show answerHide answer
| 00 | 01 | 10 | 11 | 10 | 00 | |
|---|---|---|---|---|---|---|
| Selected | ||||||
| 1 | 0 | 1 | 0 | 1 | 1 |
A MUX as a logic function generator. A -to-1 MUX can implement any function of variables. Connect the variables to the select lines, and tie each data input to the function’s output for that row (HIGH for 1, LOW for 0). The MUX then just looks up the truth table, like an FPGA LUT (Ch 3). This usually replaces several gate chips with one MUX chip.
There’s also a smaller trick, where a -to-1 MUX can do an -variable function. Put variables on the select lines and feed each data input with , , the last variable, or its complement (see Example 6.7).
Example 6.7: A MUX as a function generator
Implement (variables , , ; is the MSB) using (a) an 8-to-1 MUX and (b) a 4-to-1 MUX.
Show answerHide answer
(a) Connect , , to the three select lines. Tie , , , HIGH and , , , LOW.
(b) Connect , to , . For each pair of rows with the same , see how depends on :
| when | when | Data input | |
|---|---|---|---|
| 00 | 1 | 0 | |
| 01 | 0 | 1 | |
| 10 | 0 | 1 | |
| 11 | 1 | 0 |
That’s one 4-to-1 MUX and one inverter. (This function is , which would otherwise need two XOR-type gates.)
Demultiplexers
A demultiplexer (DEMUX) is the reverse of a MUX. It takes data from one line and sends it to one of several output lines, chosen by the select inputs. It’s also called a data distributor.
1-to-4 DEMUX: the data input goes to all four AND gates, and the select lines enable only one of them:
The unselected outputs stay at 0.
Example 6.8: Demultiplexer
Serial data arrives on the data input of a 1-to-4 DEMUX, one bit per interval, while the select lines go . Where does each bit end up?
Show answerHide answer
| Interval | 1 | 2 | 3 | 4 |
|---|---|---|---|---|
| 00 | 01 | 10 | 11 | |
| Data in | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 0 | |
| 0 | 0 | 0 | 0 | |
| 0 | 0 | 1 | 0 | |
| 0 | 0 | 0 | 1 |
Each bit only shows up on the selected output during its interval, and the other outputs stay 0.
A decoder is a DEMUX. A decoder with an enable input already has this structure. Use the decoder inputs as the select lines and the enable as the data input. For example, a 4-line-to-16-line decoder with two active-LOW chip-select inputs works as a 1-to-16 DEMUX, where one chip select carries the data and the other is held LOW to enable the chip.
Parity generators and checkers
Parity is a simple way to detect a single-bit error. An extra parity bit is sent along with the data so the total number of 1s is:
- Even parity: even
- Odd parity: odd
The key fact is that the XOR (sum without carries) of an even number of 1s is 0, and of an odd number of 1s it’s 1. So XORing all the bits together counts the 1s mod 2:
- Generator: XOR the data bits. For even parity the parity bit is ; for odd parity it’s .
- Checker: XOR all the received bits including the parity bit. For even parity, means OK and means an error (for odd parity it’s the other way around).
A single flipped bit always changes the count of 1s by one, so it always gets caught. Two flipped bits cancel out and don’t.
Example 6.9: Parity
(a) Find the even and odd parity bits for the data . (b) The byte (7 data bits + even parity bit) is received. Is there an error?
Show answerHide answer
(a) The data has four 1s (even), so .
- Even parity bit: 0 (the total stays at four 1s)
- Odd parity bit: 1 (makes five 1s)
(b) The received byte has five 1s, so the checker outputs . With even parity that means there’s an error (at least one bit flipped).
Key formulas
2-bit equality comparator
- , : the two 2-bit numbers being compared
Decoder outputs
- : number of input lines. Each output is one minterm of the inputs.
4-to-1 multiplexer
- –: data inputs
- : select inputs; the binary number is the index of the data input passed to
1-to-4 demultiplexer
- : the minterm of the select lines for output (e.g. )
Parity
- when the bits contain an odd number of 1s
Common mistakes
- Comparing magnitudes from the LSB. Always start at the MSB, because the first difference decides.
- Mixing up decoders and encoders. A decoder turns a code into one active line and an encoder turns one active line into a code.
- Reading the select lines in the wrong order. is the MSB, so selects , not .
- Thinking unselected DEMUX outputs “hold” their old value. They’re 0.
- Tying data inputs to the wrong rows for a MUX function generator (check that the select variables are wired MSB-first, same as the truth table).
- Thinking parity can correct errors or catch two-bit errors. It only detects an odd number of flipped bits.