Hardware interview practice
Binary-to-Gray converter
Write combinational SystemVerilog that converts an 8-bit binary value to reflected Gray code and reconstructs the original binary value.
Starting point
Question code
module gray8 (
input logic [7:0] bin,
output logic [7:0] gray,
output logic [7:0] bin_back
);Reviewed example
Work through one case
Input
bin=8'd7Expected output
gray=8'd4, bin_back=8'd7Binary 0000_0111 converts to Gray 0000_0100 and the prefix XOR reconstructs the input.
What to cover
Requirements
- Use gray = bin ^ (bin >> 1) for forward conversion.
- Copy the Gray MSB and prefix-XOR downward for reverse conversion.
- Keep both conversions purely combinational.
- Bound the reverse loop with a signed int index to avoid unsigned underflow.
