6.8 Magnitude Comparator: Determining A > B, A = B, A < B

Explain how a magnitude comparator produces its three mutually exclusive outputs (A>B, A=B, A<B), and apply the most-significant-differing-bit rule to compare multi-bit binary values by hand.

A magnitude comparator is a combinational circuit that compares two binary numbers, A and B, and reports their relative size through three outputs: A>B, A=B, and A<B — exactly one of which is true (logic 1) for any given pair of inputs, since two numbers must stand in exactly one of these three relationships. Comparators are essential wherever a digital system needs to make a decision based on relative magnitude rather than an exact value — sorting logic, range checking, priority arbitration, and branch conditions in a CPU (such as 'less than' or 'equal to' comparisons in conditional instructions) all rely on comparator logic at their core.

The three outputs are mutually exclusive

For any two binary numbers A and B, exactly one of three relationships holds: A is strictly greater than B, A equals B, or A is strictly less than B. A correctly designed magnitude comparator therefore produces exactly one of its three outputs (A>B, A=B, A<B) as logic 1 for any given input combination, with the other two held at logic 0 — the outputs behave like a one-hot code over the three possible outcomes.

The A=B output is the simplest to derive conceptually: two n-bit numbers are equal precisely when every corresponding pair of bits matches, which can be tested bit-by-bit with an XNOR gate per bit position (XNOR outputs 1 exactly when its two inputs match) and then ANDing all the per-bit XNOR results together — A=B is true only if every single bit position agrees.

Comparison starts at the most significant differing bit

Determining A>B or A<B for multi-bit numbers follows the same logic as comparing multi-digit decimal numbers by hand: you scan from the most significant digit toward the least significant digit, and the very first position where the two numbers differ determines the entire comparison result — whichever number has the larger bit at that first point of disagreement is the larger number overall, regardless of what any of the remaining, less-significant bits are.

This is why comparator logic is inherently structured from the most significant bit downward: a bit-by-bit comparator starts by checking the most significant bit pair; only if those bits are equal does the comparison logic need to look at the next bit pair down, and so on, until either a differing bit pair is found (which immediately settles the result) or every bit position has been checked and found equal (settling A=B).

Magnitude comparator outputs

Outputs (mutually exclusive, exactly one is 1):
  A > B
  A = B
  A < B

Comparison procedure: scan from the most significant bit (MSB) toward the least significant bit (LSB);
the first bit position where A and B differ determines the result.

Code Examples

This is the spec's own worked example. At the very first (most significant) bit position, A has a 1 where B has a 0 — that single differing bit is enough to conclude A > B immediately, without needing to examine bits 2, 1, or 0 at all, since no combination of lower bits could ever overturn a disagreement already established at a higher-order bit position. Expected result: A > B

A=1001, B=0111 → A > B, decided entirely by the most significant bit (bit 3), where A has 1 and B has 0.

Here the top two bits are equal on both sides, so the comparison logic must continue checking lower bit positions. Bit 1 is the first place A and B actually disagree, and because A's bit is 1 while B's is 0 at that position, A is larger — bit 0 never needs to be examined at all, since the outcome is already fully determined. Expected result: A > B

When higher bits are equal, comparison continues to the next lower bit until the first disagreement is found.

A = 1  0  0  1   (MSB to LSB)
B = 0  1  1  1

Bit 3 (MSB): A3=1, B3=0  -> A3 differs from B3, and A3 > B3

Since the most significant differing bit already favors A, the comparison stops here:
result is A > B, regardless of any of the remaining, lower bit positions.
A = 1  0  1  0
B = 1  0  0  1

Bit 3: A3=1, B3=1  -> equal, continue
Bit 2: A2=0, B2=0  -> equal, continue
Bit 1: A1=1, B1=0  -> A1 differs from B1, and A1 > B1

Result: A > B, decided at bit 1 (the first point of disagreement, scanning from MSB down)

Magnitude comparator block diagram

A comparator reduces two n-bit numbers to exactly one of three mutually exclusive relational outputs.

A[n-1:0]  ---->  |                     | ----> A > B
   B[n-1:0]  ---->  |  Magnitude          | ----> A = B
                     |  Comparator          | ----> A < B

  Exactly one of the three outputs is 1 for any given (A, B) pair.
  Internally: compare from MSB downward; first differing bit pair decides the result.

Important Rules

Exactly one of A>B, A=B, A<B is true for any pair of input values.

A=B requires every corresponding bit pair to match (testable with per-bit XNOR, ANDed together).

Comparison proceeds from the most significant bit toward the least significant bit.

The first bit position where A and B differ fully determines the relational result; all lower bits become irrelevant once that first disagreement is found.

Where magnitude comparators are used

Magnitude comparators underlie every conditional branch instruction in a CPU that depends on a relational test (less-than, greater-than, equal), every range-checking circuit (for example, validating that a sensor reading falls within an allowed band), priority and arbitration logic that must select the 'largest' or 'highest-priority' among several competing values, and sorting networks built from cascaded comparator stages. Small, fixed-width comparators (4-bit, 8-bit) are also available as standalone MSI (medium-scale integration) logic chips and are frequently cascaded to build comparators for wider numbers.

Comparing two multi-bit numbers by starting at the least significant bit, the way binary addition proceeds.

Start the comparison at the most significant bit and work downward; the first (highest-order) disagreement decides the result, exactly the reverse order from how addition's carries propagate.

The spec's tricky problem states this directly: comparison starts at the most significant differing bit. Starting from the wrong end can lead to concluding a relationship based on a low-order bit that a higher-order bit would have already overridden.

[tricky] For A=1001 and B=0111, what does a 4-bit magnitude comparator output, and why? A. A > B, because the most significant bit differs (A3=1, B3=0), and that single bit alone determines the entire comparison B. A < B, because B has more 1-bits in its lower three positions C. A = B, because both numbers have exactly two 1-bits total D. The result is undefined without examining every bit position individually first

A. A > B, because the most significant bit differs (A3=1, B3=0), and that single bit alone determines the entire comparison — The spec's own example confirms this: A=1001, B=0111 gives A>B. Comparison starts at the most significant differing bit; here bit 3 already disagrees (A has 1, B has 0), which alone determines A>B regardless of any lower bits.

Why must a magnitude comparator's outputs (A>B, A=B, A<B) be mutually exclusive? A. Because any two binary numbers stand in exactly one of these three relationships to each other B. Because comparator circuits are only allowed a single active output pin by convention C. Because A=B is only meaningful when A>B and A<B are both undefined D. Mutual exclusivity is not actually required; multiple outputs may be 1 simultaneously

A. Because any two binary numbers stand in exactly one of these three relationships to each other — For two real numbers (or binary values), exactly one of greater-than, equal-to, or less-than must hold — this is a basic property of ordering, not an arbitrary circuit convention, so a correctly functioning comparator always asserts exactly one of its three outputs.

For A=0110 and B=0101, determine A>B, A=B, or A<B by hand using the most-significant-differing-bit rule.

Bit 3: A3=0, B3=0, equal. Bit 2: A2=1, B2=1, equal. Bit 1: A1=1, B1=0, differ — A1 > B1. Result: A > B, decided at bit 1; bit 0 is never examined because the outcome is already settled.

Compare bit 3 first, then bit 2, and so on, stopping at the first position where the bits differ.