Two
-bit unsigned integers are equal if each pair of corresponding
bits is equal. This can be tested efficiently by constructing a
-bit equality circuit for each integer and using AND gates to
combine their results. Since
-bit equality can be implemented as a
circuit with constant size and depth
and
such circuits are
combined, this gives a circuit with linear size and depth
.
The following figures show the dependencies in building an equality circuits. There is not a one-to-one correspondence with gates and inputs. In these particular examples, the internal nodes are AND gates and the leaf nodes are one bit equality circuits for the appropriate bits.
It is important to use the balanced tree form of the expression.
If two
-bit unsigned integers are not equal, the highest order pair
of different bits determines their relative order. Suppose the highest
order bits are different. The integer with its high order bit set to
zero is less than the other. If the highest order bits are the same,
then comparing the integers is reduced to the other bits. For example,
In these two numbers, the bit corresponding to
determines the
relative ordering since it is the most significant bit that is
different. Note that even though the first number has all zeros after
this bit (minimizing it) and the second number has all ones after this
bit (maximizing it), the first number is greater than the second.
Suppose
is a circuit computing whether
.
is a degenerate case - there are no bits to examine so the two
numbers must be the same. For
, there are three possibilities for
the high-order bits. If the
bit is less than the
bit, then
the first number is less (setting all the other
bits would still
be one short of the
bit followed by zeros). If the
bit is
the same as the
bit, then the first number is less if and only
if the remainder of the first number (
) is less
than the remainder of the second number (
). In the
third case, the first number is greater than the second number. A
circuit built from this recurrence has the following structure.
This circuit has linear depth and linear size. The linear depth naturally follows from the recursion only decreasing the size by one. We can build a circuit with logarithmic depth by halving the number of bits being considered at each step instead.
Let
be a circuit computing whether
. Let
be the corresponding equality circuit. Note that
. Our previous definition of
becomes the following set of equations.
This gives an equation of linear size and logarithmic depth. Ignoring the costs of the equality circuits, the recursion gives the same structure as the equality circuit construction so the size is the same (give or take a constant factor). The cost of the equality circuits is at most linear since a equality circuit using the same splitting method would include all of the desired circuits.