Skip to question
asic.fyi
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
asic.fyi/Interview questions/Binary-to-Gray converter

Hardware interview practice

Binary-to-Gray converter

EasyClock Domain CrossingSystemVerilog

Write combinational SystemVerilog that converts an 8-bit binary value to reflected Gray code and reconstructs the original binary value.

Try it in the question bankReason first. Then compare.

Keep this exact question selected while you check your answer and review the full solution.

Practice this question →
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'd7
Expected output
gray=8'd4, bin_back=8'd7

Binary 0000_0111 converts to Gray 0000_0100 and the prefix XOR reconstructs the input.

What to cover

Requirements

  1. Use gray = bin ^ (bin >> 1) for forward conversion.
  2. Copy the Gray MSB and prefix-XOR downward for reverse conversion.
  3. Keep both conversions purely combinational.
  4. Bound the reverse loop with a signed int index to avoid unsigned underflow.
Continue practicing

Related questions

Clock Domain CrossingEight-entry asynchronous FIFO→Clock Domain CrossingGenerate and synchronize a Gray-code counter→Clock Domain CrossingImplement asynchronous FIFO full and empty logic→
asic.fyi · Learn silicon end to end.info@asic.fyi