Comparators
In previous articles, we examined multiplexers, decoders, and priority encoders.
This article examines digital comparators.
Comparators evaluate two binary words to determine mathematical equality or relative magnitude.
Every high-level conditional statement (such as if (A > B)) compiles into a hardware comparator.
Comparators introduce the most-significant-bit-first (MSB-first) evaluation architecture common to digital arithmetic units.
Equality Comparison Logic
Two individual binary bits ($A$ and $B$) are equal if both are 0 or both are 1. An XNOR gate performs this operation:
$$EQ = A \odot B = \overline{A \oplus B} = AB + \overline{A},\overline{B}$$
Table 1 lists the truth table:
| $A$ | $B$ | $A = B$ ($EQ$) |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Two $n$-bit binary words ($A$ and $B$) are equal if and only if every corresponding bit position matches. For a 4-bit equality comparator, combine the individual XNOR outputs with an AND gate:
$$EQ = (A_3 \odot B_3)(A_2 \odot B_2)(A_1 \odot B_1)(A_0 \odot B_0)$$
In general, an $n$-bit equality comparator requires $n$ XNOR gates and an $n$-input AND gate. The propagation delay is two gate levels regardless of word width.
Magnitude Comparison Principles
Evaluating relative magnitude ($A > B$ or $A < B$) requires hierarchical bit evaluation. The circuit compares bits starting from the Most Significant Bit (MSB) and proceeds downwards toward the Least Significant Bit (LSB). If the bits at position $i$ differ, they determine the comparison result immediately. If the bits at position $i$ are equal, evaluation moves to position $i-1$.
For a single bit position $i$, three mutually exclusive conditions exist:
$$G_i = A_i \overline{B_i} \quad (A_i > B_i)$$
$$L_i = \overline{A_i} B_i \quad (A_i < B_i)$$
$$E_i = A_i \odot B_i \quad (A_i = B_i)$$
Figure 1 shows the gate-level schematic for a 1-bit comparator:
Exactly one of the three outputs ($G_i, L_i, E_i$) asserts high for any input condition.
4-Bit Magnitude Comparator Logic
For two 4-bit words ($A_3 A_2 A_1 A_0$ and $B_3 B_2 B_1 B_0$), condition $A > B$ asserts if:
- Bit 3 is greater ($G_3 = 1$), OR
- Bit 3 ties ($E_3 = 1$) and bit 2 is greater ($G_2 = 1$), OR
- Bits 3 and 2 tie ($E_3 E_2 = 1$) and bit 1 is greater ($G_1 = 1$), OR
- Bits 3, 2, and 1 tie ($E_3 E_2 E_1 = 1$) and bit 0 is greater ($G_0 = 1$).
The resulting Boolean sum-of-products equation is:
$$(A > B) = G_3 + E_3 G_2 + E_3 E_2 G_1 + E_3 E_2 E_1 G_0$$
Figure 2 illustrates the hierarchical logic gate structure:
By symmetry, the condition for $A < B$ is:
$$(A < B) = L_3 + E_3 L_2 + E_3 E_2 L_1 + E_3 E_2 E_1 L_0$$
The condition for equality is:
$$(A = B) = E_3 E_2 E_1 E_0$$
Standard discrete integrated circuits (such as the 74HC85 4-bit magnitude comparator) implement this logic architecture directly.
Cascading Comparators for Wider Words
You can cascade multiple 4-bit comparators to compare wider data words. Figure 3 shows a 16-bit comparator constructed from four cascaded 74HC85 4-bit comparators.
- The least significant stage (bits 3:0) receives fixed tie conditions on its cascade inputs ($A = B$ tied to 1; $A > B$ and $A < B$ tied to 0).
- The outputs of each stage drive the cascade inputs of the next higher stage.
- The most significant stage (bits 15:12) generates the final comparison outputs.
If the upper four bits differ, the most significant stage determines the output immediately and ignores lower stages. If the upper bits tie, the stage passes the cascade inputs directly to its outputs. This ripple architecture introduces a linear propagation delay ($O(N)$). High-performance processors use tree comparators with lookahead logic to minimize delay.
Unsigned Versus Signed Comparisons
The standard comparator evaluates unsigned binary numbers. For signed two’s complement numbers, the most significant bit represents a negative weight ($-2^{n-1}$). This sign inversion modifies the comparison rules for the MSB.
- Unsigned Comparison: $A_3 = 1$ and $B_3 = 0$ means $A > B$.
- Signed Comparison: $A_3 = 1$ means $A$ is negative, and $B_3 = 0$ means $B$ is positive. Therefore, $A < B$.
In a signed comparator, the conditions for the MSB position invert:
$$G_3^{\text{signed}} = \overline{A_3} B_3$$
$$L_3^{\text{signed}} = A_3 \overline{B_3}$$
All lower-order bit positions ($i < 3$) continue to use standard unsigned magnitude comparison logic.
Because signed and unsigned comparisons require different hardware logic, computer architectures provide distinct conditional branch instructions (Table 2):
| Architecture | Signed Branch Instructions | Unsigned Branch Instructions |
|---|---|---|
| ARM | BGT, BLT, BGE, BLE |
BHI, BLO, BHS, BLS |
| RISC-V | blt, bge |
bltu, bgeu |
| x86 | jg, jl, jge, jle |
ja, jb, jae, jbe |
Memory Address Range Checking
Embedded microprocessors often verify whether a memory address falls within a protected address range.
For example, check whether address $A$ is in the range 0x8000 through 0xBFFF.
A general solution requires two comparators: $A \ge \text{0x8000}$ AND $A \le \text{0xBFFF}$.
However, analyzing the binary boundaries simplifies the circuit:
0x8000=1000_0000_0000_0000_20xBFFF=1011_1111_1111_1111_2
Every address in this range has upper bits $A_{15} A_{14} = 10_2$. No address outside this range shares this prefix. Therefore, the address range check simplifies to a single product term:
$$\text{in_range} = A_{15} \overline{A_{14}}$$
The circuit requires only one inverter and one 2-input AND gate. Aligning memory regions to powers of two eliminates full magnitude comparators.
2-Bit Comparator Truth Table
Table 3 lists all sixteen input combinations for a 2-bit magnitude comparator ($A_1 A_0$ versus $B_1 B_0$):
| $A_1$ | $A_0$ | $B_1$ | $B_0$ | $GT$ ($A > B$) | $EQ$ ($A = B$) | $LT$ ($A < B$) |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 1 | 0 |
| 0 | 0 | 0 | 1 | 0 | 0 | 1 |
| 0 | 0 | 1 | 0 | 0 | 0 | 1 |
| 0 | 0 | 1 | 1 | 0 | 0 | 1 |
| 0 | 1 | 0 | 0 | 1 | 0 | 0 |
| 0 | 1 | 0 | 1 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 0 | 0 | 1 |
| 0 | 1 | 1 | 1 | 0 | 0 | 1 |
| 1 | 0 | 0 | 0 | 1 | 0 | 0 |
| 1 | 0 | 0 | 1 | 1 | 0 | 0 |
| 1 | 0 | 1 | 0 | 0 | 1 | 0 |
| 1 | 0 | 1 | 1 | 0 | 0 | 1 |
| 1 | 1 | 0 | 0 | 1 | 0 | 0 |
| 1 | 1 | 0 | 1 | 1 | 0 | 0 |
| 1 | 1 | 1 | 0 | 1 | 0 | 0 |
| 1 | 1 | 1 | 1 | 0 | 1 | 0 |
The algebraic formulas derived from Table 3 match the MSB-first equations:
- $GT = A_1\overline{B_1} + (A_1 \odot B_1)A_0\overline{B_0}$
- $EQ = (A_1 \odot B_1)(A_0 \odot B_0)$
- $LT = \overline{A_1}B_1 + (A_1 \odot B_1)\overline{A_0}B_0$
Practice Exercises
Problem 1: Compare two unsigned 4-bit numbers $A = 1011_2$ (decimal 11) and $B = 1010_2$ (decimal 10) using MSB-first evaluation.
View Solution
Evaluate bits from MSB to LSB:
- Bit 3: $A_3 = 1$, $B_3 = 1 \implies E_3 = 1, G_3 = 0, L_3 = 0$ (Tie).
- Bit 2: $A_2 = 0$, $B_2 = 0 \implies E_2 = 1, G_2 = 0, L_2 = 0$ (Tie).
- Bit 1: $A_1 = 1$, $B_1 = 1 \implies E_1 = 1, G_1 = 0, L_1 = 0$ (Tie).
- Bit 0: $A_0 = 1$, $B_0 = 0 \implies G_0 = 1, E_0 = 0, L_0 = 0$ ($A > B$).
Evaluating the formula: $$(A > B) = G_3 + E_3 G_2 + E_3 E_2 G_1 + E_3 E_2 E_1 G_0 = 0 + 0 + 0 + (1)(1)(1)(1) = 1$$
Therefore, $A > B$.
Problem 2: Determine the simplified logic equation for decoding the address range 0x4000 through 0x7FFF on a 16-bit address bus ($A_{15} \dots A_0$).
View Solution
Convert the boundaries to binary:
0x4000=0100_0000_0000_0000_20x7FFF=0111_1111_1111_1111_2
All addresses in this range have $A_{15} A_{14} = 01_2$. The decoded range output is:
$$\text{in_range} = \overline{A_{15}} A_{14}$$
Problem 3: Explain why comparing the two’s complement 8-bit values 0xFF and 0x01 asserts $A < B$, whereas an unsigned comparison asserts $A > B$.
View Solution
- Unsigned Interpretation:
0xFFrepresents $255_{10}$ and0x01represents $1_{10}$. Because $255 > 1$, the unsigned comparison asserts $A > B$. - Signed Two’s Complement Interpretation:
0xFFrepresents $-1_{10}$ and0x01represents $+1_{10}$. Because $-1 < +1$, the signed comparison asserts $A < B$.
The comparator interprets the most significant bit ($A_7 = 1, B_7 = 0$) as negative weight in signed mode, inverting the comparison result.
In the final article of Module 1, we combine multiplexers, decoders, encoders, and comparators into larger digital systems, including an Arithmetic Logic Unit (ALU).