SourceRTL Design Directory

Comparator: An Overview

A comparator is a fundamental building block in digital design that compares two binary numbers. Comparators can be categorized based on their use and functionality.

  • Equality Comparator: it compares the two binary numbers to up a true bit or high when all the binary bits are identical and false or low when the bits are not identical.
  • Magnitude Comparator: has three functionality, to determine the relationship between the two binary numbers that are being compared. The three functionalities are greater than, less than, and equal to.

Applications of Comparators

  1. Comparators are used in sorting algorithms to compare and order numbers.
  2. In signed subtractions, it is necessary to compare the magnitudes to perform the operation.
  3. Many control systems and processors use comparators to make decisions based on numerical comparisons.
  4. Extremely helpful in looping statements.

Design: 4-bit Comparator

A 4-bit comparator consists of two inputs each of four bits and three outputs, one for each comparison. Typically, a magnitude comparator is used widely in many digital system applications. Verilog provides three operators for the three-magnitude comparator operations. They are as follows:

  1. Greater than: (A > B) checks whether A is greater than B.
  2. Less than: (A < B) checks if A is less than B.
  3. Equal to: (A = B) checks if both the values are equal.

Verilog Code: 4-bit Comparator

module comparator #(parameter n = 4)(/*AUTOARG*/
   // Outputs
   gt, lt, et,
   // Inputs
   a, b
   );
   input [n-1:0] a, b;
   output      gt,lt,et;

  /*AUTOREG*/
  /*AUTOWIRE*/

   assign et = (a == b) ? 1'b1 : 1'b0;
   assign gt = (a > b)  ? 1'b1 : 1'b0;
   assign lt = (a < b)  ? 1'b1 : 1'b0;

endmodule