Introduction to comparator: The Digital Comparator Another common and very useful combinational logic circuit is that of the Digital Comparator circuit. Digital or Binary Comparators are made up from standard AND, NOR and NOT gates that compare the digital signals present at their input terminals and produce an output depending upon the condition of those inputs. For example, along with being able to add and subtract binary numbers we need to be able to compare them and determine whether the value of input A is greater than, smaller than or equal to the value at input B etc. The digital comparator accomplishes this using several logic gates that operate on the principles of Boolean algebra. There are two main types of Digital Comparator available and these are.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Introduction to comparator:
The Digital Comparator
Another common and very useful combinational logic circuit is that of
the Digital Comparator circuit. Digital or Binary Comparators are made up
from standard AND, NOR and NOT gates that compare the digital signals
present at their input terminals and produce an output depending upon the
condition of those inputs.
For example, along with being able to add and subtract binary numbers we need
to be able to compare them and determine whether the value of input A is
greater than, smaller than or equal to the value at input B etc. The digital
comparator accomplishes this using several logic gates that operate on the
principles of Boolean algebra. There are two main types of Digital
Comparator available and these are.
1. Identity Comparator - an Identity Comparator is a digital comparator that has
only one output terminal for when A = B either "HIGH" A = B = 1 or
"LOW" A = B = 0
2. Magnitude Comparator - a Magnitude Comparator is a type of digital
comparator that has three output terminals, one each for equality, A = B greater
than, A > B and less thanA < B
The purpose of a Digital Comparator is to compare a set of variables or
unknown numbers, for example A (A1, A2, A3, .... An, etc) against that of a
constant or unknown value such as B (B1, B2, B3, .... Bn, etc) and produce an
output condition or flag depending upon the result of the comparison. For
example, a magnitude comparator of two 1-bits, (A and B) inputs would
produce the following three output conditions when compared to each other.
Which means: A is greater than B, A is equal to B, and A is less than B
This is useful if we want to compare two variables and want to produce an
output when any of the above three conditions are achieved. For example,
produce an output from a counter when a certain count number is reached.
Consider the simple 1-bit comparator below.
1-bit Digital Comparator
Then the operation of a 1-bit digital comparator is given in the following Truth
Table.
Truth Table
Inputs Outputs
B A 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
You may notice two distinct features about the comparator from the above truth
table. Firstly, the circuit does not distinguish between either two "0" or two "1"'s
as an output A = B is produced when they are both equal, either A = B =
"0" or A = B = "1". Secondly, the output condition for A = B resembles that of a
commonly available logic gate, the Exclusive-NOR or Ex-NOR function
(equivalence) on each of the n-bits giving: Q = A ⊕ B
Digital comparators actually use Exclusive-NOR gates within their design for
comparing their respective pairs of bits. When we are comparing two binary or
BCD values or variables against each other, we are comparing the "magnitude"
of these values, a logic "0" against a logic "1" which is where the
term Magnitude Comparator comes from.
As well as comparing individual bits, we can design larger bit comparators by
cascading together n of these and produce a n-bit comparator just as we did for
the n-bit adder in the previous tutorial. Multi-bit comparators can be constructed
to compare whole binary or BCD words to produce an output if one word is
larger, equal to or less than the other.
A very good example of this is the 4-bit Magnitude Comparator. Here, two 4-
bit words ("nibbles") are compared to each other to produce the relevant output
with one word connected to inputs A and the other to be compared against
connected to input B as shown below.
4-bit Magnitude Comparator
Some commercially available digital comparators such as the TTL 74LS85 or
CMOS 4063 4-bit magnitude comparator have additional input terminals that
allow more individual comparators to be "cascaded" together to compare words
larger than 4-bits with magnitude comparators of "n"-bits being produced.
These cascading inputs are connected directly to the corresponding outputs of
the previous comparator as shown to compare 8, 16 or even 32-bit words.
8-bit Word Comparator
When comparing large binary or BCD numbers like the example above, to save
time the comparator starts by comparing the highest-order bit (MSB) first. If
equality exists, A = B then it compares the next lowest bit and so on until it
reaches the lowest-order bit, (LSB). If equality still exists then the two numbers
are defined as being equal.
If inequality is found, either A > B or A < B the relationship between the two
numbers is determined and the comparison between any additional lower order
bits stops. Digital Comparator are used widely in Analogue-to-Digital
converters, (ADC) and Arithmetic Logic Units, (ALU) to perform a variety of
arithmetic operations.
Comparator:
The operation of a single bit digital comparator can be expressed as a truth
table:
Inputs Outputs
0 0 0 1 0
0 1 0 0 1
1 0 1 0 0
1 1 0 1 0
The operation of a two bit digital comparator can be expressed as a truth table:
Inputs Outputs
0 0 0 0 0 1 0
0 0 0 1 1 0 0
0 0 1 0 1 0 0
0 0 1 1 1 0 0
0 1 0 0 0 0 1
0 1 0 1 0 1 0
0 1 1 0 1 0 0
0 1 1 1 1 0 0
1 0 0 0 0 0 1
1 0 0 1 0 0 1
1 0 1 0 0 1 0
1 0 1 1 1 0 0
1 1 0 0 0 0 1
1 1 0 1 0 0 1
1 1 1 0 0 0 1
1 1 1 1 0 1 0
Implementation:
Consider two 4-bit binary numbers A and B such that
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
.
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 quality 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, 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:
(A>B) and (A < B) are output binary variables, which are equal to 1 when A>B
or A<B respectively.
Representation:
A value of a fixed-point data type is essentially an [[integer]] that is scaled by a
specific factor determined by the type. For example, the value 1.23 can be
represented as 1230 in a fixed-point data type with scaling factor of 1/1000, and
the value 1230000 can be represented as 1230 with a scaling factor of 1000.
Unlike floating-point data types, the scaling factor is the same for all values of
the same type, and does not change during the entire computation.
The scaling factor is usually a [[power (mathematics)|power]] of 10 (for human
convenience) or a power of 2 (for computational efficiency). However, other
scaling factors may be used occasionally, e.g. a time value in hours may be
represented as a fixed-point type with a scale factor of 1/3600 to obtain values
with one-second accuracy.
The maximum value of a fixed-point type is simply the largest value that can be
represented in the underlying integer type, multiplied by the scaling factor; and
similarly for the minimum value. For example, consider a fixed-point type
represented as a binary integer with ''b'' bits in [[two's complement]] format,
with a scaling factor of 1/2<sup>''f''</sup> (that is, the last ''f'' bits are fraction
bits): the minimum representable value is &minus
Operations:
To convert a number from a fixed point type with scaling factor ''R'' to another
type with scaling factor ''S'', the underlying integer must be multiplied by ''R''
and divided by ''S''; that is, multiplied by the ratio ''R''/''S''. Thus, for example,
to convert the value 1.23 = 123/100 from a type with scaling factor ''R''=1/100
to one with scaling factor ''S''=1/1000, the underlying integer 123 must be
multiplied by (1/100)/(1/1000) = 10, yielding the representation 1230/1000. If
''S'' does not divide ''R'' (in particular, if the new scaling factor ''S'' is less than
the original ''R''), the new integer will have to be [[rounding|rounded]]. The
rounding rules and methods are usually part of the language's specification.
To add or subtract two values of the same fixed-point type, it is sufficient to add
or subtract the underlying integers, and keep their common scaling factor. The
result can be exactly represented in the same type, as long as no [[arithmetic
overflow|overflow]] occurs (i.e. provided that the sum of the two integers fits in
the underlying integer type). If the numbers have different fixed-point types,
with different scaling factors, then one of them must be converted to the other
before the sum.
To multiply two fixed-point numbers, it suffices to multiply the two underlying
integers, and assume that the scaling factor of the result is the product of their
scaling factors. This operation involves no rounding. For example, multiplying
the numbers 123 scaled by 1/1000 (0.123) and 25 scaled by 1/10 (2.5) yields the
integer 123×25 = 3075 scaled by (1/1000)×(1/10) = 1/10000, that is 3075/10000
= 0.3075. If the two operands belong to the same fixed-point type, and the result
is also to be represented in that type, then the product of the two integers must
be explicitly multiplied by the common scaling factor; in this case the result
may have to be rounded, and overflow may occur. For example, if the common
scaling factor is 1/100, multiplying 1.23 by 0.25 entails multiplying 123 by 25
to yield 3075 with an intermediate scaling factor of 1/10000. This then must be
multiplied by 1/100 to yield either 31 (0.31) or 30 (0.30), depending on the
rounding method used, to result in a final scale factor of 1/100.
To divide two fixed-point numbers, one takes the integer quotient of their
underlying integers, and assumes that the scaling factor is the quotient of their
scaling factors. The first division involves rounding in general. For example,
division of 3456 scaled by 1/100 (34.56) and 1234 scaled by 1/1000 (1.234)
yields the integer 3456÷1234 = 3 (rounded) with scale factor (1/100)/(1/1000) =
10, that is, 30. One can obtain a more accurate result by first converting the
[[dividend]] to a more precise type: in the same example, converting 3456
scaled by 1/100 (34.56) to 3456000 scaled by 1/100000, before dividing by
1234 scaled by 1/1000 (1.234), would yield 3456000÷1234 = 2801 (rounded)
with scaling factor (1/100000)/(1/1000) = 1/100, that is 28.01 (instead of 290).
If both operands and the desired result are represented in the same fixed-point
type, then the quotient of the two integers must be explicitly divided by the
common scaling factor.
Binary vs. Decimal:
The two most common classes of fixed-point types are decimal and binary.
Decimal fixed-point types have a scaling factor that is a power of ten; for binary
fixed-point types it is a power of two.
Binary fixed-point types are most commonly used, because the rescaling
operations can be implemented as fast [[bit shift]]s. Binary fixed-point numbers
can represent fractional powers of two exactly, but, like binary floating-point
numbers, cannot exactly represent fractional powers of ten. If exact fractional
powers of ten are desired, then a decimal format should be used. For example,
one-tenth (0.1) and one-hundredth (0.01) can be represented only approximately
by binary fixed-point or binary floating-point representations, while they can be
represented exactly in decimal fixed-point or decimal floating-point
representations. These representations may be encoded in many ways,
including [[Binary-coded decimal|BCD]].
Precision loss and overflow:
Because fixed point operations can produce results that have more bits than the
[[operand]]s, there is possibility for information loss. For instance, the result of
fixed point multiplication could potentially have as many bits as the sum of the
number of bits in the two operands. In order to fit the result into the same
number of bits as the operands, the answer must be [[Rounding|rounded]] or
[[truncated]]. If this is the case, the choice of which bits to keep is very
important. When multiplying two fixed point numbers with the same format, for
instance with <math>I</math> integer bits, and <math>Q</math> fractional
bits, the answer could have up to <math>2I</math> integer bits, and
<math>2Q</math> fractional bits.
For simplicity, many fixed-point multiply procedures use the same result format
as the operands. This has the effect of keeping the middle bits; the
<var>I</var>-number of least significant integer bits, and the <var>Q</var>-
number of most significant fractional bits. Fractional bits lost below this value
represent a precision loss which is common in fractional multiplication. If any
integer bits are lost, however, the value will be radically inaccurate. Some
model-based fixed-point packages<ref name="vsi-fp">VisSim Fixed-Point User