Magnitude comparator

Compares two unsigned buses A and B and reports the result on three separate outputs: A>B, A=B and A<B. Exactly one of them is HIGH at any moment, because two numbers are always either greater, equal or lesser. It answers the question a subtractor only implies, and it answers it without producing a difference you would then have to interpret.

Basic

Reading the three flags

Feed A and B from two multi-bit inputs and put an LED on each of the three outputs. Change either number and watch the lit LED move between the three flags. The table below uses 2-bit values to keep it short; the behaviour is the same at any width.

ABA>BA=BA<B
00010
10100
23001
33010
Intermediate

A threshold detector

Hold B fixed with a DIP switch and drive A from a counter. The A>B output now goes HIGH the moment the count passes your threshold, and stays HIGH after that. Combine A>B with A=B through an OR gate when you want the "greater than or equal" condition, which the comparator does not provide as its own pin.

Advanced

Equality is the cheap part

It is worth knowing what the comparator costs you. Equality alone is inexpensive: XNOR each pair of bits and AND the results, since two numbers are equal exactly when every bit position matches. Ordering is the expensive part, because it has to be decided from the most significant differing bit downwards, which is why a magnitude comparator grows with width in a way an equality detector does not. If your design only ever needs A=B, building it from XNORs is smaller than placing a full comparator.

Citations

  • Mano, M. M., & Ciletti, M. D. (2018). Digital design: With an introduction to the Verilog HDL, VHDL, and SystemVerilog (6th ed.). Pearson. Reference 1
  • Wakerly, J. F. (2018). Digital design: Principles and practices (5th ed.). Pearson.