Reflected Binary Conversion
Given a number, implement the conversion from binary to reflected binary.
Reflected binary is an alternate binary representation where numbers in sequence only change a single bit at a time. For example, the numbers 0 through 4 are represented in binary and reflected binary below. This is also known as Gray code.
| Number | Binary | Reflected |
|---|---|---|
| 0 | 0000 | 0000 |
| 1 | 0001 | 0001 |
| 2 | 0010 | 0011 |
| 3 | 0011 | 0010 |
| 4 | 0100 | 0110 |
uint toReflected(uint x) {
return x ^ (x >> 1);
}