What is a Digital Comparator?
A digital comparator, also called a magnitude comparator, is a hardware device that compares two binary numbers and determines whether one number is greater than, less than, or equal to the other.
An XNOR gate acts as the simplest comparator because its output becomes “1” only when both input bits are equal. A voltage comparator is the analog equivalent of a digital comparator. Many microcontrollers include analog comparators that can be read or used to trigger interrupts.
Digital comparators operate using logic gates based on Boolean algebra. They compare two binary inputs—A and B—and determine whether:
- A > B
- A < B
- A = B
Digital comparators are classified into two major types:
Types of Digital Comparators
1. Identity Comparator
An identity comparator checks only whether two inputs are equal. It has two inputs and one output.
For two inputs P and Q:
- If P = Q → Output = HIGH
- If P ≠ Q → Output = LOW
2. Magnitude Comparator
A magnitude comparator compares the complete magnitude of two binary numbers and produces outputs for:
- Greater than (>)
- Less than (<)
- Equal to (=)
1-Bit Magnitude Comparator
A 1-bit comparator compares two single-bit inputs. It has two inputs (A and B) and three outputs indicating whether A > B, A = B, or A < B.
Truth Table for 1-Bit Comparator
| A | B | A < B | A = B | A > B |
|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 0 |
| 1 | 0 | 0 | 0 | 1 |
| 1 | 1 | 0 | 1 | 0 |
Boolean Expressions
- A > B: A·B’
- A < B: A’·B
- A = B: A’B’ + AB
2-Bit Magnitude Comparator
A 2-bit comparator compares two 2-bit numbers. There are 16 possible input combinations. Based on the comparison, the outputs indicate P > Q, P = Q, or P < Q.
If both inputs are the same, the equality output becomes HIGH. If P’s binary value is greater than Q’s, the P > Q output is HIGH, and vice-versa.
n-Bit Magnitude Comparator
For higher-bit comparisons (4-bit, 8-bit, etc.), creating truth tables becomes impractical. Instead, general mathematical expressions help determine how many combinations correspond to each output.
Total Possible Combinations
For two n-bit inputs:
Total combinations = 22n
Equal Output (P = Q)
Two n-bit numbers are equal when all bits match.
P = Q occurs 2n times.
Not Equal Output (P ≠ Q)
P ≠ Q occurs 22n − 2n times.
Greater Than / Less Than
Since the total non-equal cases are split evenly:
- P > Q: (22n − 2n) / 2
- P < Q: (22n − 2n) / 2
Example (3-bit Inputs)
- Total combinations = 26 = 64
- P = Q occurs 23 = 8 times
- P ≠ Q occurs 64 − 8 = 56 times
- P > Q occurs 28 times
- P < Q occurs 28 times
Applications of Digital Comparators
- Used in authentication systems such as password verification and biometric matching
- Used in process controllers and servo motor control
- Used in comparing sensor data (temperature, pressure, etc.) with reference values
- Used in address decoding circuits in computers
- Used in arithmetic logic units (ALUs) for decision-making operations