A comparator is a combinational circuit that compares two binary values and determines their relationship: greater than (A > B), less than (A < B), or equal (A = B). It is a fundamental building block in digital systems, used in processors, arithmetic units, and decision-making circuits.
In this guide, we’ll walk through the design of 1-bit, 2-bit, and 4-bit comparators using digital logic gates. By the end, you’ll understand how to construct efficient comparator circuits and simplify their logic equations. Let’s get started.
Update (2025): This guide has been reviewed and updated to ensure accuracy and relevance. All concepts, equations, and circuit designs remain valid for modern digital logic applications.
Contents
1-bit Comparator: Logic and Circuit Design
A 1-bit comparator compares two single bits.
A | B | A>B | A<B | A=B |
0 | 0 | 0 | 0 | 1 |
0 | 1 | 0 | 1 | 0 |
1 | 0 | 1 | 0 | 0 |
1 | 1 | 0 | 0 | 1 |
Let’s apply a shortcut to find the equations for each of the cases. Normally, we can use a K-map. But this shortcut is efficient and handy when you understand it.
For A>B, there is only one case when the output is high when A=1 and B=0. Let’s call this X. We can write the equation as follows
X(which stands for A>B) = AB’
Similarly, denote A<B with Y and A=B with Z.
Since Y is high when A=0 and B=1, we get the following equation
Y = A’B
Since Z is high in two cases, there will be an OR gate. This is because the logic behind an OR gate is that a high output can be achieved in one or more cases. Z is high when A=0 and B=0, it is also high when A=1 and B=1. Therefore,
Z = A’B’ + AB
This is similar to the equation of an EXNOR gate. Hence,
Z = AB
2-Bit Comparator: Truth Table and Circuit Implementation
Let’s plot the truth table for a 2-bit comparator
A1 | A0 | B1 | B0 | A>B | A<B | A=B |
0 | 0 | 0 | 0 | 0 | 0 | 1 |
0 | 0 | 0 | 1 | 0 | 1 | 0 |
0 | 0 | 1 | 0 | 0 | 1 | 0 |
0 | 0 | 1 | 1 | 0 | 1 | 0 |
0 | 1 | 0 | 0 | 1 | 0 | 0 |
0 | 1 | 0 | 1 | 0 | 0 | 1 |
0 | 1 | 1 | 0 | 0 | 1 | 0 |
0 | 1 | 1 | 1 | 0 | 1 | 0 |
1 | 0 | 0 | 0 | 1 | 0 | 0 |
1 | 0 | 0 | 1 | 1 | 0 | 0 |
1 | 0 | 1 | 0 | 0 | 0 | 1 |
1 | 0 | 1 | 1 | 0 | 1 | 0 |
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 | 0 | 1 |
The shortcut that we saw above can be used here too. But notice that since we have four variables (A1, A0, B1, B0) and each of the three outputs is high at least four times, the equations that we will get will have four terms of 4 variables. At least. So, though applying the shortcut is possible, we won’t.
K-maps come in handy in situations like these. Because it is possible to achieve the most straightforward equation using them, and remember, the simpler the equation, the lesser the logic gates required. That is the aim of any designing process – to obtain the simplest hardware implementation.
Using K-maps
we get the following equations
A>B = A1B1′ + B0′(A0B1′ + A0A1)
A<B = B1A1′ + B0B1A0′ + A1’A0’B0
A=B = .
4Bit Comparator: Step-by-Step Design Approach
The truth table for a 4-bit comparator would have 4^4 = 256 rows. So we will do things a bit differently here. We will compare each bit of the two 4-bit numbers, and based on that comparison and the weight of their positions, we will draft a truth table.
A3B3 | A2B2 | A1B1 | A0B0 | A>B | A<B | A=B |
A3>B3 | x | x | x | 1 | 0 | 0 |
A3<B3 | x | x | x | 0 | 1 | 0 |
A3=B3 | A2>B2 | x | x | 1 | 0 | 0 |
A3=B3 | A2<B2 | x | x | 0 | 1 | 0 |
A3=B3 | A2=B2 | A1>B1 | x | 1 | 0 | 0 |
A3=B3 | A2=B2 | A1<B1 | x | 0 | 1 | 0 |
A3=B3 | A2=B2 | A1=B1 | A0>B0 | 1 | 0 | 0 |
A3=B3 | A2=B2 | A1=B1 | A0<B0 | 0 | 1 | 0 |
A3=B3 | A2=B2 | A1=B1 | A0=B0 | 0 | 0 | 1 |
Now let’s derive the equations for the three outputs.
A=B
Recall the 1-bit comparator circuit we saw above. The equation for the A=B condition was AB. Let’s call this x. Taking a look at the truth table above, A=B is true only when (A3=B3 and A2=B2 and A1=B1 and A0=B0). Hence
Z (A=B) = A3B3 . A2B2 . A1.B1 . A0.B0 = x3x2x1x0
A>B
Since there are multiple occasions where this particular condition is high, we will OR (add) each of those individual occasions. We find the first instance of A>B at the top of the table where A3>B3. For this to be possible in a binary system, A3 has to be equal to 1, and B3 has to be equal to 0. Since there are only 0s and 1s in a binary system. We can represent this as A3.B3′. Moving on to the next instance of A>B, we can see that it occurs at A3=B3 and A2>B2. From the equation for A=B above, A3=B3 can be represented as x3. And this entire instance can be written as x3A2B2′. Similarly, deriving equations for the remaining instances, we get the following equation
X(A>B) = A3B3′ + x3A2B2′ + x3x2A1B1′ + x3x2x1A0B0′
A<B
Employing the same principles we used above, we get the following equation
Y(A<B) = A3’B3 + X3A2’B2 + X3X2A1’B1 + X3X2X1A0’B0
Designing the circuit based on the above equations, we get the following logic diagram for a 4-bit comparator.
How do I remember this?
This is the exact question I had when I first studied this truth table. I felt that this truth table was made only because whoever made it knew that it had to be made this way. How would I, as a student, be expected to devise a new system for a truth table? The answer is, you don’t have to. You are entirely free to do it the old way with 256 rows. But this is a more natural way to deal with when you have many variables that will end up in a vast truth table. You can remember it and maybe use it elsewhere when the need arises. If not, that’s okay, too; you can bookmark this page and refer to it when you are tasked with making a huge truth table.
Note:
- This post is a part of our free course on Digital Electronics and Digital Logic Design.
- We will use all of the equations above when we code these comparators in our VHDL and Verilog course.
Frequently Asked Questions (FAQs) about Digital Comparator
What is a digital comparator in logic design?
A digital comparator is a combinational circuit used in digital electronics to compare two binary numbers. It determines whether one number is greater than (A > B), less than (A < B), or equal to (A = B).
How does a 1-bit comparator work?
A 1-bit comparator compares two single-bit binary inputs (A and B) and produces three possible outputs:
- A > B (A is greater)
- A < B (A is smaller)
- A = B (A and B are equal)
The logic equations for a 1-bit comparator circuit are:
- A > B = AB{\prime}
- A < B = A{\prime}B
- A = B = A \oplus B
What is the truth table for a 2-bit comparator?
A 2-bit comparator compares two 2-bit binary numbers (A1A0 and B1B0) and outputs whether one is greater, smaller, or equal. The truth table shows all possible input combinations and their corresponding outputs.
How do you design a 4-bit comparator using logic gates?
A 4-bit comparator works by comparing each corresponding bit in two 4-bit numbers. The circuit is designed using AND, OR, and NOT gates, along with XNOR gates for equality checks. The final output is derived based on the most significant bit (MSB) comparison first, followed by lower bits.
Why use Karnaugh maps (K-maps) for designing a comparator?
Karnaugh maps (K-maps) simplify Boolean expressions, reducing the number of required logic gates in a comparator circuit. This minimizes hardware complexity and improves efficiency, especially for 2-bit and 4-bit comparators.
What are the applications of comparators in digital circuits?
Comparators are widely used in:
- Microprocessors and microcontrollers (for decision-making)
- Analog-to-digital converters (ADC)
- Data sorting and priority encoding
- Digital signal processing (DSP)
- Memory addressing and error detection