Skip to content
BetterDL

Comparator

Also called: digital comparator, binary comparator, comparators

A combinational circuit that compares two binary numbers and reports whether they are equal, and often which one is larger.

A comparator answers questions like "is A equal to B?" and "is A bigger?" for two binary numbers. It comes in two flavors:

  • An equality comparator only says equal or not. Two numbers are equal when every bit position matches, so it uses one XNOR per position (1 = this position matches) and ANDs the results.
  • A magnitude comparator also reports which number is larger, usually on three outputs G (A > B), E (A = B) and L (A < B). Exactly one of them is 1.

For single bits the whole story fits in a few gates:

  • G = (A is 1, B is 0)
  • E = (the bits agree)
  • L = (A is 0, B is 1)

For multi-bit numbers, compare the way you compare words in a dictionary: start at the MSB. The first position where the numbers differ decides which is larger. Lower bits only matter while every higher bit is equal.

Where it shows up: branch instructions ("jump if A < B"), sorting hardware, thermostats and timers that compare a count with a target. A CPU often does comparisons by subtracting and checking the status flags instead.

ABGEL

Worked examples

Example

Comparing 4-bit numbers

A = 1011 (11), B = 1001 (9). Work from the MSB down.

  1. 1.

    Bit 3: 1 vs 1. Equal, keep going.

  2. 2.

    Bit 2: 0 vs 0. Equal, keep going.

  3. 3.

    Bit 1: 1 vs 0. First difference, and A has the 1.

  4. 4.

    So A > B: G = 1, E = 0, L = 0. Bit 0 never gets looked at.

Example

The 1-bit comparator, row by row

Check the three equations on all four input pairs.

  1. 1.

    A B = 00: G = 0, E = 1, L = 0.

  2. 2.

    A B = 01: G = 0, E = 0, L = = 1.

  3. 3.

    A B = 10: G = = 1, E = 0, L = 0.

  4. 4.

    A B = 11: G = 0, E = 1, L = 0. Every row lights exactly one output.

Common mistakes

  • Comparing from the LSB. The most significant differing bit decides, so start at the top.

  • Building multi-bit greater-than as an OR of per-bit terms. 01 vs 10 would then claim A > B because of bit 0.

  • Using XOR for equality. XOR is 1 when bits differ; equality needs XNOR (or XOR followed by NOR).

Practice Comparator

Interactive questions with instant feedback and a worked solution for every wrong answer.

Learn it step by step

Comparator is taught in Combinational Logic and Adders and ALUs.