NATIONAL UNIVERSITY OF MODERN LANGUAGES
DLD LAB REPORT-9
Muhammad Akif Fareed
System ID: NUML-F24-30279
Roll NO: 9248115
Submitted To:
Mr. Yasir Siddiqi
November 25, 2025
Objective:
To design and implement magnitude comparator using logic gates and using verilog.
Theory:
Binary code and Gray code are two commonly used coding schemes in digital systems.
Binary Code:
Binary code represents numbers using base–2 numeral [Link] binary values may
differ in multiple bits.
Gray Code:
Gray code is a binary numeral system where two successive values differ in only one bit.
Gray code is widely used in:
Rotary encoders
Digital communication
Error correction
K-Maps
Conversion Rule:
A 4-bit binary input B3 B2 B1 B0 converts to Gray code G3 G2 G1 G0 as follows:
G3 = B3
G2 = B3 XOR B2
G1 = B2 XOR B1
G0 = B1 XOR B0
This ensures only one bit changes at a time when moving from one value to the next.
Truth Table:
Binary (B3 B2 B1 B0) Gray (G3 G2 G1 G0)
0000 0000
0001 0001
0010 0011
0011 0010
0100 0110
0101 0111
0110 0101
0111 0100
1000 1100
Binary (B3 B2 B1 B0) Gray (G3 G2 G1 G0)
1001 1101
1010 1111
1011 1110
1100 1010
1101 1011
1110 1001
1111 1000
Verilog Code:
Procedure:
1. Write the Verilog code for the binary-to-Gray converter.
2. Create a test bench to apply all 16 combinations of binary inputs.
3. Run simulation using ModelSim Verilog simulator.
4. Observe output waveforms.
5. Verify that the output matches the Gray code truth table.
Results
The Verilog design successfully converted binary input to correct Gray code.
The simulated waveform matched the expected truth table.
Only one bit changed between consecutive Gray outputs, confirming proper Gray code
behavior.
Conclusion
The experiment successfully demonstrated the implementation of a 4-bit Binary to Gray Code
converter using Verilog.
The truth table and simulation verified that the design functions correctly, and the XOR-logic-
based approach generated accurate Gray code output.