4 bit Binary to Gray code and Gray code to Binary converter in Verilog Gray codes are non-weighted codes, where two successive values differ only on one bit. Through this post, I want to share two simple gate level Verilog codes for converting binary number to Gray and vice versa. Counter (3) DAC (1) decoders (1).
Active1 year, 5 months ago
This binary to gray conversion code is an example from a book. Can anybody explain:
I am not able to understand this specific XOR operation for converting to gray code from binary.
Example on EDAplayground.
Morgan16.4k55 gold badges4747 silver badges7272 bronze badges
Vaibhav SundriyalVaibhav Sundriyal32233 gold badges88 silver badges1717 bronze badges
5 Answers
The
^
is an XOR (exclusive or) operator. The
i
's are just indexes for current and last position in the loop. The (unary) Xor can also be used as a reduction operator to generate parity of words:
MorganMorgan16.4k55 gold badges4747 silver badges7272 bronze badges
A more compact solution (though functionally identical) is this:
This avoids the generate block and the loops but still does the bitwise XOR on all of the bits except the MSB.
Where you do need the loop is going in the other direction (gray to binary):
nguthrienguthrie1,77811 gold badge2020 silver badges3636 bronze badges
This is just the way the conversion works. If you follow this image, you'll see basically the same operation as your Verilog code.
charlieamercharlieamer
Simple Binary to gray conversion Data flow Model Verilog code:
module bintogray(input [3:0]bin,output [3:0]gray);
assign gray[3] = bin[3];
assign gray[2] = bin[3]^bin[2];
assign gray[1] = bin[2]^bin[1];
assign gray[0] = bin[1]^bin[0];
endmodule
karthick Ramasamykarthick Ramasamy
ManishManish