Gray Code Converter
beginner · combinational · 65 XP
Sign in to write your solution, run it in the simulator and submit it for grading.
Sign in or create a free account →Problem
Gray Code Converter
Implement Binary-to-Gray conversion for a 4-bit input.
**Binary to Gray:** Each Gray bit is the XOR of the binary bit with the bit above it:
```
gray[3] = bin[3]
gray[2] = bin[3] ^ bin[2]
gray[1] = bin[2] ^ bin[1]
gray[0] = bin[1] ^ bin[0]
```
**Interface**
| Port | Direction | Width | Description |
|------|-----------|-------|-------------|
| `bin[3:0]` | input | 4 | Binary input |
| `gray[3:0]` | output | 4 | Gray code output |
**Why it matters:** Adjacent Gray codes differ in exactly one bit. Used in rotary encoders, ADCs, and CDC pointer generation (async FIFOs).
**Verification:** Sweep 0→15 and check that each consecutive Gray output differs in exactly one bit.
Constraints
- Use four assign statements (one per output bit).
Examples
Input: bin=4'b0001
Output: gray=4'b0001
Input: bin=4'b0010
Output: gray=4'b0011
Input: bin=4'b0011
Output: gray=4'b0010
Common questions
- What does the Gray Code Converter problem ask you to build?
- Gray Code Converter Implement Binary-to-Gray conversion for a 4-bit input. **Binary to Gray:** Each Gray bit is the XOR of the binary bit with the bit above it: ``` gray[3] = bin[3] gray[2] = bin[3] ^ bin[2] gray[1] = bin[2] ^ bin[1] gray[0] = bin[1] ^ bin[0] ``` **Interface** | Port | Direction | W…
- What constraints does Gray Code Converter have?
- Use four assign statements (one per output bit).
- What is an example input and output for Gray Code Converter?
- Input bin=4'b0001 gives output gray=4'b0001.
- Which Verilog concepts does Gray Code Converter practise?
- combinational, gray-code, interview.