next up previous
Next: Addition of Integers (Ripple Up: Integer Representation and Arithmetic Previous: Unsigned Integers

Comparison of Integers

Two $n$-bit unsigned integers are equal if each pair of corresponding bits is equal. This can be tested efficiently by constructing a $1$-bit equality circuit for each integer and using AND gates to combine their results. Since $1$-bit equality can be implemented as a circuit with constant size and depth $3$ and $n$ such circuits are combined, this gives a circuit with linear size and depth $3 +
\left\lceil \lg n \right\rceil$.

\begin{eqnarray*}
A & = & a_{n-1} a_{n-2} \ldots a_1 a_0 \\
B & = & b_{n-1} b_{...
...ft( a_1 = b_1
\right) \land \left( a_0 = b_0 \right) \right) \\
\end{eqnarray*}



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.

\psfig{figure=eq-line-8.ps,height=5in}

It is important to use the balanced tree form of the expression.

\psfig{figure=eq-tree-8.ps,width=5in}

If two $n$-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,

$2^5$ $2^4$ $2^3$ $2^2$ $2^1$ $2^0$
$1$ $0$ $1$ $1$ $0$ $0$
$1$ $0$ $1$ $0$ $1$ $1$
      $\uparrow$    

In these two numbers, the bit corresponding to $2^2$ 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 $\mathit{lt}(n)$ is a circuit computing whether $a_{n-1} a_{n-2} \ldots
a_1 a_0 < b_{n-1} b_{n-2} \ldots b_1 b_0$.

\begin{eqnarray*}
a_n < b_n & = & \left( a_n = 0 \right) \land \left( b_n = 1 \r...
...\left( \left(
a_n = b_n \right) \land \mathit{lt}(n) \right) \\
\end{eqnarray*}



$lt(0)$ is a degenerate case - there are no bits to examine so the two numbers must be the same. For $lt(n+1)$, there are three possibilities for the high-order bits. If the $a_n$ bit is less than the $b_n$ bit, then the first number is less (setting all the other $a_i$ bits would still be one short of the $b_n$ bit followed by zeros). If the $a_n$ bit is the same as the $b_n$ bit, then the first number is less if and only if the remainder of the first number ( $a_{n-1} \ldots a_0$) is less than the remainder of the second number ( $b_{n-1} \ldots b_0$). In the third case, the first number is greater than the second number. A circuit built from this recurrence has the following structure.

\psfig{figure=lt-line-8.ps,height=5in}

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 $\mathit{lt_2}(i,j)$ be a circuit computing whether $a_j a_{j-1}
\ldots a_{i+1} a_i < b_j b_{j-1} \ldots b_{i+1} b_i$. Let $\mathit{eq_2}(i,j)$ be the corresponding equality circuit. Note that $\mathit{lt}(n) = \mathit{lt_2}(0,n-1)$. Our previous definition of $\mathit{lt}(n)$ becomes the following set of equations.

\begin{eqnarray*}
\mathit{lt_2}(i,i) & = & a_i < b_i \\
\mathit{lt_2}(i,j) & = ...
...(\mathit{eq_2}((i+j)/2+1,j) \land \mathit{lt_2}(i, (i+j)/2)) \\
\end{eqnarray*}



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.

\psfig{figure=lt-tree-8.ps,width=7in}


next up previous
Next: Addition of Integers (Ripple Up: Integer Representation and Arithmetic Previous: Unsigned Integers
Jeffrey Considine 2001-05-01