Takes two input vectors (so, like, one or more bits) A and B, and determines if A is less than, greater than, or equal to B.

There are 4 total permutations for 1-bit inputs, which makes 1 bit comparators super easy!

But for more than 1 bit, things get spicy.

  • For instance if we’re talking about 2 bit inputs, A == B only when and so that becomes obtuse quite fast
  • To check if A is greater than B we check if
    1. . In that case, is a given, or
      1. This looks like
    2. and .
      1. This looks like less than is similar!

The expression is true when the first bit is equal (replace the 1’s with 0’s to get when the second bit is equal)

General comparators

For a β€œgeneral” comparator we need to define some things:

Equality:

  • For equality of any bit, we have
  • That gives us

> and <

  • For any digit, if the previous are equal, and the one we’re on, satisfies:
    • and , then no matter what bits after follow it
    • and , then for similar reason to above
  • That gives us, for any sized input stream:
      • The first check is your base case (if the first bit of A is bigger than the second of B, then boom you’re done)
        • the is defined from before
      • The second checks if the bits before are equal. if they are, we also compare the bit at.
      • This repeats until we’re checking for every bit before the last bit; if they’re all equal, then compare the bits we’re at.
    • The same is reflected on of course

Example of 4-bit comparator expressions

Subtraction instead

The more bits you have, the more complex comparing becomes. Therefore, subtracting might be the play. Simply subtract A and B, and if the sign changes, you can determine if A < B or A > B!