This is the gray code. The changing rule can be:

- update the rightmost big
- for xxx100…00, it can change the digit x which is next to 100..00

See lc1611

The property of gray code, is that each two adjacent number only have 1 bit difference.

For example, in decimal, if we want to update from 011 to 100, the electronic element may change like 100->101->111->100. There is potential ephemeral number 101, 111. In gray code, this won’t happen. Because each two adjacent number only has 1 bit difference.

Decimal | Binary | Gray |
---|---|---|

0 | 0000 | 0000 |

1 | 0001 | 0001 |

2 | 0010 | 0011 |

3 | 0011 | 0010 |

4 | 0100 | 0110 |

5 | 0101 | 0111 |

6 | 0110 | 0101 |

7 | 0111 | 0100 |

8 | 1000 | 1100 |

9 | 1001 | 1101 |

10 | 1010 | 1111 |

11 | 1011 | 1110 |

12 | 1100 | 1010 |

13 | 1101 | 1011 |

14 | 1110 | 1001 |

15 | 1111 | 1000 |

Transformation from decimal to gray code. See here.

- keep the leftmost bit same.
- For each following bit, xor the 2 adjacent decimal bits

(10011001)gc -> (11010101)d

Transformation from gray code to decimal. See here

- keep the leftmost bit same.
- For each following bit, xor current gray code bit and decimal bit

(11010101)d -> (10011001)gc