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?

 

 

Code

module bin2gray4(
    input [3:0] bin_in,
    output reg [3:0] gray_out
);

// 1st approach 
    // assign gray_out = bin_in ^ (bin_in >> 1);

// 2nd approach
integer i;

always @(*) begin
gray_out[3] = bin_in[3];
for (i = 3; i > 0; i = i - 1) begin
    gray_out[i-1] = bin_in[i] ^ bin_in[i-1];
end
end

endmodule
Was this helpful?
Upvote
Downvote