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 Input :n=3 Output :000 001 011 010 110 111 101 100
Another approach of Generate n-bit Gray Codes has been already discussed.
Approach is to get gray code of binary number using XOR and Right shift operation.
In this way we can get the gray code of corresponding binary number. So, observation is that to get i’th bit gray code, we have to do XOR operation of i’th bit and previous i-1’th bit. So by doing right shift, i-1’th bit and i’th bit come at same position and the XOR result of i’th bit and i-1’th bit will be the i’th bit gray code.That’s the intuition behind right shift operation.
000 001 011 010 110 111 101 100
- Generate n-bit Gray Codes
- A backtracking approach to generate n bit Gray Codes
- Gray to Binary and Binary to Gray conversion
- Binary to Gray code using recursion
- Decimal Equivalent of Gray Code and its Inverse
- Code Converters - Binary to/from Gray Code
- Generate 0 and 1 with 25% and 75% probability
- Count of even and odd set bit with array element after XOR with K
- Count of even set bits between XOR of two arrays
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits
- Check if a Number is Odd or Even using Bitwise Operators
- Bitwise OR( | ) of all even number from 1 to N
- Bitwise AND of all the odd numbers from 1 to N
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.