# Generate n-bit Gray Codes | Set 2

Given a number n, generate bit patterns from 0 to 2^n-1 such that successive patterns differ by one bit.

Examples:

```Input : n=2
Output :00 01 11 10

Input :n=3
Output :000 001 011 010 110 111 101 100
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.

• 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 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.

## C++

 `// C++ program to generate n-bit ` `// gray codes ` `#include ` `using` `namespace` `std; ` ` `  `// Function to convert decimal to binary ` `void` `decimalToBinaryNumber(``int` `x, ``int` `n) ` `{ ` `    ``int``* binaryNumber = ``new` `int``(x); ` `    ``int` `i = 0; ` `    ``while` `(x > 0) { ` `        ``binaryNumber[i] = x % 2; ` `        ``x = x / 2; ` `        ``i++; ` `    ``} ` ` `  `    ``// leftmost digits are filled with 0 ` `    ``for` `(``int` `j = 0; j < n - i; j++) ` `        ``cout << ``'0'``; ` ` `  `    ``for` `(``int` `j = i - 1; j >= 0; j--) ` `        ``cout << binaryNumber[j]; ` `} ` ` `  `// Function to generate gray code ` `void` `generateGrayarr(``int` `n) ` `{ ` `    ``int` `N = 1 << n; ` `    ``for` `(``int` `i = 0; i < N; i++) { ` ` `  `        ``// generate gray code of corresponding ` `        ``// binary number of integer i. ` `        ``int` `x = i ^ (i >> 1); ` ` `  `        ``// printing gray code ` `        ``decimalToBinaryNumber(x, n); ` ` `  `        ``cout << endl; ` `    ``} ` `} ` ` `  `// Drivers code ` `int` `main() ` `{ ` `    ``int` `n = 3; ` `    ``generateGrayarr(n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to generate  ` `// n-bit gray codes ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to convert ` `// decimal to binary ` `static` `void` `decimalToBinaryNumber(``int` `x,  ` `                                  ``int` `n) ` `{ ` `    ``int` `[]binaryNumber = ``new` `int``[x]; ` `    ``int` `i = ``0``; ` `    ``while` `(x > ``0``) ` `    ``{ ` `        ``binaryNumber[i] = x % ``2``; ` `        ``x = x / ``2``; ` `        ``i++; ` `    ``} ` ` `  `    ``// leftmost digits are ` `    ``// filled with 0 ` `    ``for` `(``int` `j = ``0``; j < n - i; j++) ` `        ``System.out.print(``'0'``); ` ` `  `    ``for` `(``int` `j = i - ``1``;  ` `             ``j >= ``0``; j--) ` `        ``System.out.print(binaryNumber[j]); ` `} ` ` `  `// Function to generate ` `// gray code ` `static` `void` `generateGrayarr(``int` `n) ` `{ ` `    ``int` `N = ``1` `<< n; ` `    ``for` `(``int` `i = ``0``; i < N; i++)  ` `    ``{ ` ` `  `        ``// generate gray code of  ` `        ``// corresponding binary  ` `        ``// number of integer i. ` `        ``int` `x = i ^ (i >> ``1``); ` ` `  `        ``// printing gray code ` `        ``decimalToBinaryNumber(x, n); ` ` `  `        ``System.out.println(); ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `n = ``3``; ` `    ``generateGrayarr(n); ` `} ` `} ` ` `  `// This code is contributed ` `// by anuj_67. `

## Python3

 `# Python program to generate ` `# n-bit gray codes ` ` `  `# Function to convert ` `# decimal to binary ` `def` `decimalToBinaryNumber(x, n): ` `    ``binaryNumber ``=` `[``0``]``*``x; ` `    ``i ``=` `0``; ` `    ``while` `(x > ``0``): ` `        ``binaryNumber[i] ``=` `x ``%` `2``; ` `        ``x ``=` `x ``/``/` `2``; ` `        ``i ``+``=` `1``; ` ` `  `    ``# leftmost digits are ` `    ``# filled with 0 ` `    ``for` `j ``in` `range``(``0``, n ``-` `i): ` `        ``print``(``'0'``, end``=``""); ` ` `  `    ``for` `j ``in` `range``(i ``-` `1``, ``-``1``, ``-``1``): ` `        ``print``(binaryNumber[j], end``=``""); ` ` `  `# Function to generate ` `# gray code ` `def` `generateGrayarr(n): ` `    ``N ``=` `1` `<< n; ` `    ``for` `i ``in` `range``(N): ` `         `  `        ``# generate gray code of ` `        ``# corresponding binary ` `        ``# number of integer i. ` `        ``x ``=` `i ^ (i >> ``1``); ` ` `  `        ``# printing gray code ` `        ``decimalToBinaryNumber(x, n); ` ` `  `        ``print``(); ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``n ``=` `3``; ` `    ``generateGrayarr(n); ` ` `  `# This code is contributed by 29AjayKumar `

## C#

 `// C# program to generate  ` `// n-bit gray codes ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to convert ` `// decimal to binary ` `static` `void` `decimalToBinaryNumber(``int` `x,  ` `                                  ``int` `n) ` `{ ` `    ``int` `[]binaryNumber = ``new` `int``[x]; ` `    ``int` `i = 0; ` `    ``while` `(x > 0) ` `    ``{ ` `        ``binaryNumber[i] = x % 2; ` `        ``x = x / 2; ` `        ``i++; ` `    ``} ` ` `  `    ``// leftmost digits are ` `    ``// filled with 0 ` `    ``for` `(``int` `j = 0; j < n - i; j++) ` `    ``Console.Write(``'0'``); ` ` `  `    ``for` `(``int` `j = i - 1;  ` `            ``j >= 0; j--) ` `        ``Console.Write(binaryNumber[j]); ` `} ` ` `  `// Function to generate ` `// gray code ` `static` `void` `generateGrayarr(``int` `n) ` `{ ` `    ``int` `N = 1 << n; ` `    ``for` `(``int` `i = 0; i < N; i++)  ` `    ``{ ` ` `  `        ``// generate gray code of  ` `        ``// corresponding binary  ` `        ``// number of integer i. ` `        ``int` `x = i ^ (i >> 1); ` ` `  `        ``// printing gray code ` `        ``decimalToBinaryNumber(x, n); ` ` `  `        ``Console.WriteLine(); ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main ()  ` `{ ` `    ``int` `n = 3; ` `    ``generateGrayarr(n); ` `} ` `} ` ` `  `// This code is contributed ` `// by anuj_67. `

Output:

```000
001
011
010
110
111
101
100
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Improved By : vt_m, 29AjayKumar

Article Tags :
Practice Tags :

5

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.