Prev Problem
Next Problem

17. Binary to Gray Code Converter

Back To All Submissions
Previous Submission
Next Submission

Solving Approach

How do you plan to solve it?

Gray code can be generated by XORing the binary value with itself shifted right by one bit.
This produces the required pattern where each successive value changes only one bit.
Using the expression bin_in ^ (bin_in >> 1) avoids writing each bit manually.
It results in a cleaner, shorter, and scalable implementation.

 

Code

/*Write your code here*/
module bin2gray4(
    input [3:0] bin_in,
    output [3:0] gray_out
);
    assign gray_out = bin_in ^ (bin_in >> 1);
endmodule
Was this helpful?
Upvote
Downvote