Binary Numbers is default way to store numbers, but in many applications binary numbers are difficult to use and a variation of binary numbers is needed. This is where Gray codes are very useful.
Gray code has property that two successive numbers differ in only one bit because of this property gray code does the cycling through various states with minimal effort and used in K-maps, error correction, communication etc.
How to generate n bit Gray Codes?
Following is 2-bit sequence (n = 2) 00 01 11 10 Following is 3-bit sequence (n = 3) 000 001 011 010 110 111 101 100 And Following is 4-bit sequence (n = 4) 0000 0001 0011 0010 0110 0111 0101 0100 1100 1101 1111 1110 1010 1011 1001 1000
n-bit Gray Codes can be generated from list of (n-1)-bit Gray codes using following steps.
- Let the list of (n-1)-bit Gray codes be L1. Create another list L2 which is reverse of L1.
- Modify the list L1 by prefixing a ‘0’ in all codes of L1.
- Modify the list L2 by prefixing a ‘1’ in all codes of L2.
- Concatenate L1 and L2. The concatenated list is required list of n-bit Gray codes.
- 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 that index and previous index.
- The Most Significant Bit (MSB) of the binary code is always equal to the MSB of the given binary number.
- Other bits of the output binary code can be obtained by checking gray code bit at that index. If current gray code bit is 0, then copy previous binary code bit, else copy invert of previous binary code bit.
- Code Converters - Binary to/from Gray Code
- Binary to Gray code using recursion
- Decimal Equivalent of Gray Code and its Inverse
- A backtracking approach to generate n bit Gray Codes
- Generate n-bit Gray Codes | Set 2
- Generate n-bit Gray Codes
- Program for Binary To Decimal Conversion
- Program for Decimal to Binary Conversion
- Decimal to binary conversion without using arithmetic operators
- Case conversion (Lower to Upper and Vice Versa) of a string using BitWise operators in C/C++
- Print all substring of a number without any conversion
- Program for decimal to hexadecimal conversion
- Program for Celsius To Fahrenheit conversion
- Postfix to Prefix Conversion
- Number from a range [L, R] having Kth minimum cost of conversion to 1 by given operations
- Meta Binary Search | One-Sided Binary Search
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Check if binary representations of 0 to N are present as substrings in given binary string
- Construct a Maximum Binary Tree from two given Binary Trees
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
Please refer Generate n-bit Gray Codes for detailed program.
How to Convert Binary To Gray and Vice Versa?
Binary : 0011 Gray : 0010 Binary : 01001 Gray : 01101
In computer science many a times we need to convert binary code to gray code and vice versa. This conversion can be done by applying following rules :
Binary to Gray conversion :
Gray to binary conversion :
Below is the implementation of above steps.
Gray code of 01001 is 01101 Binary code of 01101 is 01001
Binary to Gray using Bitwise Operators
This article is contributed by Utkarsh Trivedi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.