A digital comparator or magnitude comparator is a
hardware electronic device that takes two numbers as input in
binary
Binary may refer to:
Science and technology Mathematics
* Binary number, a representation of numbers using only two digits (0 and 1)
* Binary function, a function that takes two arguments
* Binary operation, a mathematical operation that ta ...
form and determines whether one number is greater than, less than or equal to the other number. Comparators are used in
central processing unit
A central processing unit (CPU), also called a central processor, main processor or just processor, is the electronic circuitry that executes instructions comprising a computer program. The CPU performs basic arithmetic, logic, controlling, an ...
s (CPUs) and
microcontroller
A microcontroller (MCU for ''microcontroller unit'', often also MC, UC, or μC) is a small computer on a single VLSI integrated circuit (IC) chip. A microcontroller contains one or more CPUs ( processor cores) along with memory and programma ...
s (MCUs). Examples of digital comparator include the CMOS 4063 and 4585 and the TTL 7485 and 74682.
An
XNOR gate is a basic comparator, because its output is "1" only if its two input bits are equal.
The
analog
Analog or analogue may refer to:
Computing and electronics
* Analog signal, in which information is encoded in a continuous variable
** Analog device, an apparatus that operates on analog signals
*** Analog electronics, circuits which use analo ...
equivalent of digital comparator is the
voltage comparator. Many
microcontroller
A microcontroller (MCU for ''microcontroller unit'', often also MC, UC, or μC) is a small computer on a single VLSI integrated circuit (IC) chip. A microcontroller contains one or more CPUs ( processor cores) along with memory and programma ...
s have analog comparators on some of their inputs that can be read or trigger an
interrupt
In digital computers, an interrupt (sometimes referred to as a trap) is a request for the processor to ''interrupt'' currently executing code (when permitted), so that the event can be processed in a timely manner. If the request is accepted ...
.
Implementation

Consider two 4-bit binary numbers A and B so
Here each subscript represents one of the digits in the numbers.
;Equality
The binary numbers A and B will be equal if all the pairs of significant digits of both numbers are equal, i.e.,
,
,
and
Since the numbers are binary, the digits are either 0 or 1 and the boolean function for equality of any two digits
and
can be expressed as
we can also replace it by
XNOR gate in
digital electronics
Digital electronics is a field of electronics involving the study of digital signals and the engineering of devices that use or produce them. This is in contrast to analog electronics and analog signals.
Digital electronic circuits are usual ...
.
is 1 ''only if''
and
are equal.
For the equality of A and B, all
variables (for i=0,1,2,3) must be 1.
So the equality condition of A and B can be implemented using the
AND operation as
The binary variable (A=B) is 1 only if all pairs of digits of the two numbers are equal.
;Inequality
In order to manually determine the greater of two binary numbers, we inspect the relative magnitudes of pairs of significant digits, starting from the
most significant bit
In computing, bit numbering is the convention used to identify the bit positions in a binary number.
Bit significance and indexing
In computing, the least significant bit (LSB) is the bit position in a binary integer representing the binary ...
, gradually proceeding towards lower significant bits until an inequality is found. When an inequality is found, if the corresponding bit of A is 1 and that of B is 0 then we conclude that A>B.
This sequential comparison can be expressed logically as: