Electrical Engineering › Section T · Combinational logic › Topic T.3

Comparators, decoders, encoders, and code converters

A thermostat compares two numbers, a memory chip picks one location out of millions, a keypad turns a pressed key into a code and a display turns a code back into a digit. Four families of combinational building blocks do these jobs.

Equality comparators

A comparator tells you how two binary numbers relate. The simplest question is whether they are equal. An XNOR gate answers it for one bit, since its output is 1 exactly when its two inputs match. For two 4-bit numbers, use one XNOR per bit position and AND the four results:

(A = B) = \overline{A_3 \oplus B_3}\cdot\overline{A_2 \oplus B_2}\cdot\overline{A_1 \oplus B_1}\cdot\overline{A_0 \oplus B_0}

One mismatched bit pulls the AND output to 0. Equality comparators check whether an address on a bus matches a device's own address, or whether a counter has reached a target value.

Magnitude comparators

A magnitude comparator also reports \(A > B\) and \(A < B\). The logic copies how you compare numbers by eye: start at the most significant bit, and the first position where the bits differ decides. For 2-bit numbers, A is greater if \(A_1 = 1\) and \(B_1 = 0\), or if the top bits are equal and \(A_0 = 1\) while \(B_0 = 0\):

(A > B) = A_1\overline{B_1} + \overline{A_1 \oplus B_1}\,A_0\overline{B_0}

The \(A < B\) output is the same with A and B swapped. The 74HC85 is a 4-bit magnitude comparator with all three outputs.

Worked example: comparing and cascading

Compare \(A = 1010_2\) with \(B = 1001_2\). 1. Bit 3: both 1, equal, so look further. 2. Bit 2: both 0, equal. 3. Bit 1: \(A_1 = 1\), \(B_1 = 0\), so \(A > B\) and the lower bit no longer matters. 4. Check: 10 is greater than 9. To compare 8-bit numbers, cascade two 4-bit comparators. The 74HC85 has cascading inputs labelled \(A > B\), \(A = B\) and \(A < B\). The low-order chip's three outputs feed the high-order chip's cascading inputs, and the high-order chip uses them only when its own four bits are equal. The low-order chip's cascading inputs are tied to the "equal" state.

Decoders

A decoder detects which code is present on its inputs. To detect one code, AND the bits that should be 1 with the complements of those that should be 0: the code 1011 is detected by \(A_3\overline{A_2}A_1A_0\), and using a NAND instead gives an active-LOW output. A full binary decoder does this for every code: n inputs give \(2^n\) outputs, exactly one active at a time. Typical parts are the 3-line-to-8-line 74HC138 and the 4-line-to-16-line 74HC154. Both have active-LOW outputs and enable inputs that must be asserted before any output goes active.

Worked example: a 3-line-to-8-line decoder

A 3-to-8 decoder with active-LOW outputs \(\overline{Y_0}\) to \(\overline{Y_7}\) receives \(A_2A_1A_0 = 101\) with its enables asserted. 1. The input is \(101_2 = 5\). 2. So \(\overline{Y_5}\) goes LOW and the other seven stay HIGH: reading \(\overline{Y_7}\) to \(\overline{Y_0}\), the outputs are 1101 1111. 3. If an enable is removed, all eight outputs go HIGH, whatever the input. 4. Expansion: two of these make a 4-to-16 decoder. Send \(A_2A_1A_0\) to both, and use the new top bit \(A_3\) to enable one chip when it is 0 and the other, through an inverter, when it is 1. Memory address decoding is built this way.

BCD and 7-segment decoders

A BCD-to-decimal decoder, such as the 74HC42, has 4 inputs and 10 outputs, one per decimal digit; the six codes above 1001 select nothing. A BCD-to-7-segment decoder drives the segments a to g of a numeric display. Its outputs are active-LOW when it drives a common-anode display, whose segments light when pulled LOW. Multi-digit displays use zero suppression: a ripple-blanking connection between digits blanks leading zeros, so a 4-digit display reads 42 rather than 0042, while a single 0 still shows. The BCD code itself is covered in "Hexadecimal, octal, BCD, and error codes".

Encoders and priority

An encoder does the reverse of a decoder: one active input out of many produces the code for that input. A decimal-to-BCD encoder has 9 inputs, for digits 1 to 9, and 4 outputs; digit 0 is the case where no input is active. Each output is the OR of the inputs whose code has a 1 in that bit: \(A_0 = 1 + 3 + 5 + 7 + 9\), meaning the OR of those input lines. If two keys are pressed at once, a plain encoder outputs a meaningless mixture. A priority encoder, such as the 74HC147, outputs the code of the highest-numbered active input and ignores the rest. Its inputs and outputs are active-LOW.

Worked example: a priority encoder

Keys 2 and 6 are pressed together on a keypad feeding a decimal-to-BCD priority encoder with active-LOW inputs and outputs. 1. Inputs 2 and 6 are LOW; all others are HIGH. 2. The highest active input is 6, so input 2 is ignored. 3. In BCD, 6 is 0110. 4. The outputs are active-LOW, so they show the complement: \(\overline{A_3}\,\overline{A_2}\,\overline{A_1}\,\overline{A_0} = 1001\). 5. Check: inverting 1001 gives back 0110, which is 6, and a plain encoder would have mixed the codes 0010 and 0110.

Code converters

Code converters change one code into another. BCD to binary uses the weights of each BCD bit: the tens digit's bits are worth 80, 40, 20 and 10. So \(0010\,0111_{\text{BCD}}\) (27) is \(20 + 7 = 27 = 11011_2\). The Gray rules of "Hexadecimal, octal, BCD, and error codes" become XOR circuits. The Gray most significant bit is wired straight through, and each other bit comes from one XOR of neighbouring binary bits, \(G_i = B_{i+1} \oplus B_i\), so 4 bits need 3 gates: \(1101_2\) becomes Gray 1011. Going back, \(B_i = B_{i+1} \oplus G_i\), each XOR fed by the binary bit just found: Gray 1011 gives 1, 1, 0, 1.

Common mistakes to avoid

First, comparing magnitudes from the least significant bit: the most significant differing bit decides. Second, forgetting that decoder outputs are usually active-LOW, so the selected line is the one at 0. Third, leaving decoder enables unasserted and wondering why no output responds. Fourth, using a plain encoder where two inputs can be active together; only a priority encoder gives a valid code. Fifth, in Gray conversion, XORing Gray bits with each other when converting back: each new binary bit uses the binary bit just found.

Key takeaways

XNOR gates ANDed together detect equality; a magnitude comparator works from the most significant bit down, and 4-bit chips cascade through their A > B, A = B and A < B inputs. A decoder activates one of \(2^n\) outputs, usually active-LOW, once enabled, and two decoders with an enable bit make a larger one. BCD-to-7-segment decoders drive displays, with zero suppression for leading zeros. Encoders reverse decoders, and a priority encoder picks the highest active input. XOR gates convert between binary and Gray code.

Flashcards

Flashcard

XNOR

1-bit equality comparator

Output 1 when the bits match.

Test your understanding

Question 1 of 5

A magnitude comparator receives A = 0110 and B = 0101. Which bit position first decides the result, and what is it?

Question 2 of 5

A 3-line-to-8-line decoder with active-LOW outputs is enabled and receives the input 011. Which output is LOW?

Question 3 of 5

A decimal-to-BCD priority encoder with active-LOW outputs has inputs 4 and 9 active together. What do its four outputs show?

Question 4 of 5

What is the Gray code for the binary number 0110?

Question 5 of 5

Two 4-bit comparators are cascaded to compare 8-bit numbers. When does the high-order chip use the result from the low-order chip?

← Previous topicNext topic →