4-bit Binary to Gray Code Converter

Easy

Problem Statement

Implement a 4-bit binary-to-Gray-code converter. Ports: - `bin` — input, 4 bits (`bin[3:0]`), a standard binary value - `gray` — output, 4 bits (`gray[3:0]`), the corresponding Gray code Gray code has the property that consecutive values differ in exactly one bit, which is why it's used for synchronizing counters/pointers across clock domains and for rotary encoders. The standard binary-to-Gray conversion is defined bit-by-bit as follows: - `gray[3] = bin[3]` (the most significant bit is unchanged) - `gray[2] = bin[3] XOR bin[2]` - `gray[1] = bin[2] XOR bin[1]` - `gray[0] = bin[1] XOR bin[0]` In other words, each Gray code bit (other than the MSB) is the XOR of the corresponding binary bit and the binary bit one position above it. This circuit is purely combinational — `gray` must update immediately whenever `bin` changes, with no clock and no internal state involved.

Verilog