*57*

# Gray Code

The **Gray Code **is a sequence of binary number systems, which is also known as **reflected binary code**. The reason for calling this code as reflected binary code is the first N/2 values compared with those of the last N/2 values in reverse order. In this code, two consecutive values are differed by one bit of binary digits. Gray codes are used in the general sequence of hardware-generated binary numbers. These numbers cause ambiguities or errors when the transition from one number to its successive is done. This code simply solves this problem by changing only one bit when the transition is between numbers is done.

The gray code is a very light weighted code because it doesnâ€™t depend on the value of the digit specified by the position. This code is also called a cyclic variable code as the transition of one value to its successive value carries a change of one bit only.

### How to generate Gray code?

The prefix and reflect method are recursively used to generate the Gray code of a number. For generating gray code:

- We find the number of bits required to represent a number.
- Next, we find the code for 0, i.e., 0000, which is the same as binary.
- Now, we take the previous code, i.e., 0000, and change the most significant bit of it.
- We perform this process reclusively until all the codes are not uniquely identified.
- If by changing the most significant bit, we find the same code obtained previously, then the second most significant bit will be changed, and so on.

### Process of generating Gray Code

### Gray Code Table

Decimal Number | Binary Number | Gray Code |
---|---|---|

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 |