Gray Code Encoder and Decoder
Rotary encoders and clock-domain-crossing pointers use Gray code so exactly one bit changes per step. Implement conversion in both directions for 8-bit values.
Constraints
- `uint8_t to_gray(uint8_t binary)` and `uint8_t from_gray(uint8_t gray)`.
- from_gray(to_gray(x)) must equal x for all x.
- No loops in to_gray.
- No lookup tables.
- No library calls.
1#include <stdint.h>2 3uint8_t to_gray(uint8_t binary);4uint8_t from_gray(uint8_t gray);