- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

The reflected binary code or Gray code is an ordering of the binary numeral system such that two successive values differ in only one bit (binary digit). Gray codes are very useful in the normal sequence of binary numbers generated by the hardware that may cause an error or ambiguity during the transition from one number to the next. So, the Gray code can eliminate this problem easily since only one bit changes its value during any transition between two numbers.

Gray codes are used in rotary and optical encoders, Karnaugh maps, and error detection. The hamming distance of two neighbours Gray codes is always 1 and also first Gray code and last Gray code also has Hamming distance is always 1, so it is also called *Cyclic codes*.

**Using Karnaugh (K) - map −**You can construct Gray codes using other methods but they may not be performed in parallel like given above method. For example, 3 bit Gray codes can be contracted using K-map which is given as following below:

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

0 | 000 | 000 |

1 | 001 | 001 |

2 | 010 | 011 |

3 | 011 | 010 |

4 | 100 | 110 |

5 | 101 | 111 |

6 | 110 | 101 |

7 | 111 | 100 |

**Using Reflect and Prefix method** −

*n*-bit Gray code can be generated recursively using reflect and prefix method which is explained as following below.

- Generate code for n=1: 0 and 1 code.
- Take previous code in sequence: 0 and 1.
- Add reversed codes in the following list: 0, 1, 1 and 0.
- Now add prefix 0 for original previous code and prefix 1 for new generated code: 00, 01, 11, and 10.

Therefore, Gray code 0 and 1 are for Binary number 0 and 1 respectively. Gray codes: 00. 01, 11, and 10 are for Binary numbers: 00, 01, 10, and 11 respectively. Similarly you can construct Gray code for 3 bit binary numbers:

**Using Exclusive-Or (⊕) operation −**

This is very simple method to get Gray code from Binary number. These are following steps for n-bit binary numbers −

- The most significant bit (MSB) of the Gray code is always equal to the MSB of the given Binary code.
- Other bits of the output Gray code can be obtained by XORing binary code bit at the index and previous index.

For example, for 3-bit binary number, let Binary digits are b_{2} , b_{1} , b_{0}, where b_{2} is the most significant bit (MSB) and b0 is the least significant bit (LSB) of Binary. Gray code digits are g_{2} , g_{1} , g_{0}, where g_{2} is the most significant bit (MSB) and g_{0} is the least significant bit (LSB) of Gray code.

Binary b _{2 }b_{1 }b_{0} | Gray Code g _{2 }g_{1 }g_{0} |
---|---|

000 | 000 |

001 | 001 |

010 | 011 |

011 | 010 |

100 | 110 |

101 | 111 |

110 | 101 |

111 | 100 |

Therefore, you solve boolean expression using k-map, you will get g_{2}=b_{2}, g_{1}=b_{1}⊕b_{2}, and g_{0}=b_{0}⊕b_{1}.

Similarly, you can convert n-bit (b_{n}b_{(n-1)}...b_{2}b_{1}b_{0}) Binary number into Gray code (g_{n}g_{(n-1)}...g_{2}g_{1}g_{0}). For least significant bit (LSB) g_{0}=b_{0}⊕b_{1}, g_{1}=b_{1}⊕b_{2}, g_{2}=b_{1}⊕b_{2} , …. g_{(n-1)}=b_{(n-1)}⊕b_{n}, g_{n}=b_{n}.

**Example** −Convert Binary number 111010 into Gray code.

So, according above algorithm,

g_{0}=b_{0}⊕b_{1} = 0⊕1 = 1

g_{1}=b_{1}⊕b_{2} = 1⊕0 = 1

g_{2}=b_{2}⊕b_{3} = 0⊕1 = 1

g_{3}=b_{3}⊕b_{4} = 1⊕1 = 0

g_{4}=b_{4}⊕b_{5} = 1⊕1 = 0

g_{5}=b_{5} = 1 = 1

So, Gray will be 100111.

- Related Questions & Answers
- Conversion of Gray Code to Binary
- Python Program to Convert Gray Code to Binary
- Python Program to Convert Binary to Gray Code
- Binary to Gray code using recursion in C program
- What is Gray code?
- Gray Code in C++
- Decimal to Binary conversion
- 8085 program to convert gray to binary
- 8085 program to convert binary numbers to gray
- Binary to BCD conversion in 8051
- BCD to binary conversion in 8051
- Binary Tree to Binary Search Tree Conversion in C++
- Program to convert gray code for a given number in python
- 8085 code to convert binary number to ASCII code
- C Program for Decimal to Binary Conversion?

Advertisements