Given a number n, generate bit patterns from 0 to 2^n-1 such that successive patterns differ by one bit.
Input: n=2 Output: 00 01 11 10 Every adjacent element of gray code differs only by one bit. So the n bit grey codes are: 00 01 11 10 Input: n=3 Output: 000 001 011 010 110 111 101 100 Every adjacent element of gray code differs only by one bit. So the n bit gray codes are: 000 001 011 010 110 111 101 100
Another approach of Generate n-bit Gray Codes has already been discussed.
The idea is to get gray code of binary number using XOR and Right shift operation.
- The first bit(MSB) of the gray code is same as the first bit(MSB) of binary number.
- The second bit(from left side) of the gray code equals to XOR of first bit(MSB) and second bit(2nd MSB) of the binary number.
- The third bit(from left side) of the gray code equals to XOR of the second bit(2nd MSB) and third bit(3rd MSB) and so on..
In this way, the gray code can be calculated for the corresponding binary number. So, it can be observed that the ith element can be formed by bitwise XOR of i and floor(i/2) which is equal to the bitwise XOR of i and (i >> 1) i.e., i right-shifted by 1. By performing this the MSB of the binary number is kept intact and all the other bits are performed bitwise XOR with its adjacent higher bit.
000 001 011 010 110 111 101 100
- Time Complexity: O(n).
Only one traversal from 0 to (n-1) is needed.
- Auxiliary Space: O(log x).
A space of (log x) is required for binary representation of (x).
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.
- Generate n-bit Gray Codes
- A backtracking approach to generate n bit Gray Codes
- Gray to Binary and Binary to Gray conversion
- Code Converters - Binary to/from Gray Code
- Binary to Gray code using recursion
- Decimal Equivalent of Gray Code and its Inverse
- Generate 0 and 1 with 25% and 75% probability
- Generate an Array such with elements maximized through swapping bits
- Generate first K multiples of N using Bitwise operators
- Minimum cost to generate any permutation of the given string
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Check whether the number has only first and last bits set | Set 2
- Disjoint Set Union on trees | Set 1
- Disjoint Set Union on trees | Set 2
- Prime Number of Set Bits in Binary Representation | Set 1
- Sort an array according to count of set bits | Set 2
- Count total set bits in all numbers from 1 to n | Set 2
- Count total set bits in all numbers from 1 to N | Set 3
- Turn off the rightmost set bit | Set 2
- Position of rightmost set bit
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.