1-Bit and 4-Bit Comparator Design in Verilog
Advertisement
This document provides Verilog HDL code for both 1-bit and 4-bit comparators. Comparators are fundamental digital circuits used to determine the relationship between two input values: whether one is less than, equal to, or greater than the other.
1-Bit Comparator
Symbol
The following is the symbol for a 1-bit comparator.

Truth Table
Here’s the truth table for a 1-bit comparator, detailing the output (L, E, G) based on inputs ‘a’ and ‘b’:
| a | b | L | E | G |
|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 0 |
| 0 | 1 | 1 | 0 | 0 |
| 1 | 0 | 0 | 0 | 1 |
| 1 | 1 | 0 | 1 | 0 |
- L: a < b
- E: a = b
- G: a > b
Verilog Code
module b_comp1 (a, b, L, E, G);
input a, b;
output L, E, G;
wire s1, s2;
not X1(s1, a);
not X2(s2, b);
and X3(L, s1, b);
and X4(G, s2, a);
xnor X5(E, a, b);
endmodule
Simulation Result
The simulation results demonstrate the functionality of the 1-bit comparator.

4-Bit Comparator
Below is the symbol representing a 4-bit comparator.

module comp(a, b, aeqb, agtb, altb);
input [3:0] a, b;
output aeqb, agtb, altb;
reg aeqb, agtb, altb;
always @(a or b)
begin
aeqb = 0;
agtb = 0;
altb = 0;
if (a == b)
aeqb = 1;
else if (a > b)
agtb = 1;
else
altb = 1;
end
endmodule
Simulation Result-2
The simulation result confirms the correct operation of the 4-bit comparator.

Explore Verilog Logic Gates, Mux & Encoders
- Verilog HDL Code for All Logic Gates
- 2-to-4 Decoder (Verilog HDL Code)
- 4-to-1 Multiplexer and 1-to-4 Demultiplexer (Verilog Code)
- Verilog Code: 1-to-4 Demultiplexer
- 8-to-1 Multiplexer (Verilog HDL Code)
- 8-to-3 Encoder Without Priority (Verilog)
- 8-to-3 Priority Encoder (Verilog)
- 8-to-3 Priority Encoder (Verilog Code)
- 1-Bit and 4-Bit Comparator Design (Verilog)
Explore Verilog Flip-Flops, Counters & Registers
- T, D, SR, JK Flip Flop (Verilog HDL Code)
- D Flip Flop Synchronous Reset (Verilog)
- D Flip Flop Without Reset (Verilog)
- Verilog Code for Binary Up-Down Counter
- Verilog HDL: BCD & Gray Counters
- 4-bit Down Counter (Verilog Code Test Bench)
- 4-bit Binary Asynchronous Reset Counter (Verilog)
- 4-bit Binary Synchronous Reset Counter (Verilog)
- 4-bit BCD Asynchronous Reset Counter (Verilog)
- 4-bit BCD Synchronous Reset Counter (Verilog Code)
- 4-bit Mod-13 Counter (Verilog Code Test Bench)
- Shift Left / Shift Right Register (Verilog)
- Parallel Load Shift Left Register (Verilog Code)
- PRBS Generator (Verilog Code Test Bench)
Explore Verilog Math, Processing & Memory
- Full Adder (Verilog HDL Code)
- Verilog Code: Half Adder, Half Subtractor, Full Subtractor
- 32-Bit ALU (Verilog Code)
- RAM / ROM (Verilog Code)
- Asynchronous FIFO (Verilog Code Test Bench)
- Asynchronous FIFO Design (Verilog Code and Explanation)
- Mealy / Moore Machine (Verilog)
- Low Pass FIR Filter (Verilog Code)
- 4-bit Binary to Gray Counter Converter in Verilog
