Binary Number is the default way to store numbers, but in many applications, binary numbers are difficult to use and a variety of binary numbers is needed. This is where Gray codes are very useful.
Gray code has a 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 is 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 a list of (n-1)-bit Gray codes using the following steps.
- Let the list of (n-1)-bit Gray codes be L1. Create another list L2 which is the 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 the required list of n-bit Gray codes.
Please refer Generate n-bit Gray Codes for a detailed program.
How to Convert Binary To Gray and Vice Versa?
Binary : 0011
Gray : 0010
Binary : 01001
Gray : 01101
In computer science many a time we need to convert binary code to gray code and vice versa. This conversion can be done by applying the following rules :
Binary to Gray conversion :
- 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.

Binary code to gray code conversion
Gray to binary conversion :
- The Most Significant Bit (MSB) of the binary code is always equal to the MSB of the given gray code.
- Other bits of the output binary code can be obtained by checking the gray code bit at that index. If the current gray code bit is 0, then copy the previous binary code bit, else copy the invert of the previous binary code bit.

Gray code to binary code conversion
Below is the implementation of the above steps.
C++
#include <iostream>
using namespace std;
char xor_c( char a, char b) { return (a == b) ? '0' : '1' ; }
char flip( char c) { return (c == '0' ) ? '1' : '0' ; }
string binarytoGray(string binary)
{
string gray = "" ;
gray += binary[0];
for ( int i = 1; i < binary.length(); i++) {
gray += xor_c(binary[i - 1], binary[i]);
}
return gray;
}
string graytoBinary(string gray)
{
string binary = "" ;
binary += gray[0];
for ( int i = 1; i < gray.length(); i++) {
if (gray[i] == '0' )
binary += binary[i - 1];
else
binary += flip(binary[i - 1]);
}
return binary;
}
int main()
{
string binary = "01001" ;
cout << "Gray code of " << binary << " is "
<< binarytoGray(binary) << endl;
string gray = "01101" ;
cout << "Binary code of " << gray << " is "
<< graytoBinary(gray) << endl;
return 0;
}
|
Java
import java.io.*;
class code_conversion
{
char xor_c( char a, char b)
{
return (a == b) ? '0' : '1' ;
}
char flip( char c)
{
return (c == '0' ) ? '1' : '0' ;
}
String binarytoGray(String binary)
{
String gray = "" ;
gray += binary.charAt( 0 );
for ( int i = 1 ; i < binary.length(); i++)
{
gray += xor_c(binary.charAt(i - 1 ),
binary.charAt(i));
}
return gray;
}
String graytoBinary(String gray)
{
String binary = "" ;
binary += gray.charAt( 0 );
for ( int i = 1 ; i < gray.length(); i++)
{
if (gray.charAt(i) == '0' )
binary += binary.charAt(i - 1 );
else
binary += flip(binary.charAt(i - 1 ));
}
return binary;
}
public static void main(String args[])
throws IOException
{
code_conversion ob = new code_conversion();
String binary = "01001" ;
System.out.println( "Gray code of " +
binary + " is " +
ob.binarytoGray(binary));
String gray = "01101" ;
System.out.println( "Binary code of " +
gray + " is " +
ob.graytoBinary(gray));
}
}
|
Python3
def xor_c(a, b):
return '0' if (a = = b) else '1' ;
def flip(c):
return '1' if (c = = '0' ) else '0' ;
def binarytoGray(binary):
gray = "";
gray + = binary[ 0 ];
for i in range ( 1 , len (binary)):
gray + = xor_c(binary[i - 1 ],
binary[i]);
return gray;
def graytoBinary(gray):
binary = "";
binary + = gray[ 0 ];
for i in range ( 1 , len (gray)):
if (gray[i] = = '0' ):
binary + = binary[i - 1 ];
else :
binary + = flip(binary[i - 1 ]);
return binary;
binary = "01001" ;
print ( "Gray code of" , binary, "is" ,
binarytoGray(binary));
gray = "01101" ;
print ( "Binary code of" , gray, "is" ,
graytoBinary(gray));
|
C#
using System;
class GFG {
static char xor_c( char a, char b)
{
return (a == b) ? '0' : '1' ;
}
static char flip( char c)
{
return (c == '0' ) ? '1' : '0' ;
}
static String binarytoGray(String binary)
{
String gray = "" ;
gray += binary[0];
for ( int i = 1; i < binary.Length; i++) {
gray += xor_c(binary[i - 1],
binary[i]);
}
return gray;
}
static String graytoBinary(String gray)
{
String binary = "" ;
binary += gray[0];
for ( int i = 1; i < gray.Length; i++) {
if (gray[i] == '0' )
binary += binary[i - 1];
else
binary += flip(binary[i - 1]);
}
return binary;
}
public static void Main()
{
String binary = "01001" ;
Console.WriteLine( "Gray code of "
+ binary + " is "
+ binarytoGray(binary));
String gray = "01101" ;
Console.Write( "Binary code of "
+ gray + " is "
+ graytoBinary(gray));
}
}
|
PHP
<?php
function xor_c( $a , $b )
{
return ( $a == $b ) ? '0' : '1' ;
}
function flip( $c )
{
return ( $c == '0' ) ? '1' : '0' ;
}
function binarytoGray( $binary )
{
$gray = "" ;
$gray .= $binary [0];
for ( $i = 1; $i < strlen ( $binary ); $i ++)
{
$gray .= xor_c( $binary [ $i - 1], $binary [ $i ]);
}
return $gray ;
}
function graytoBinary( $gray )
{
$binary = "" ;
$binary .= $gray [0];
for ( $i = 1; $i < strlen ( $gray ); $i ++)
{
if ( $gray [ $i ] == '0' )
$binary .= $binary [ $i - 1];
else
$binary .= flip( $binary [ $i - 1]);
}
return $binary ;
}
$binary = "01001" ;
print ( "Gray code of " . $binary . " is " .
binarytoGray( $binary ) . "\n" );
$gray = "01101" ;
print ( "Binary code of " . $gray . " is " .
graytoBinary( $gray ));
?>
|
Javascript
<script>
function xor_c(a, b){ return (a == b) ? '0' : '1' ; }
function flip(c){ return (c == '0' ) ? '1' : '0' ; }
function binarytoGray(binary)
{
let gray = "" ;
gray += binary[0];
for (let i = 1; i < binary.length; i++)
{
gray += xor_c(binary[i - 1], binary[i]);
}
return gray;
}
function graytoBinary(gray)
{
let binary = "" ;
binary += gray[0];
for (let i = 1; i < gray.length; i++)
{
if (gray[i] == '0' )
binary += binary[i - 1];
else
binary += flip(binary[i - 1]);
}
return binary;
}
let binary = "01001" ;
document.write( "Gray code of " + binary + " is " +
binarytoGray(binary) + "</br>" );
let gray = "01101" ;
document.write( "Binary code of " + gray + " is " +
graytoBinary(gray));
</script>
|
Output
Gray code of 01001 is 01101
Binary code of 01101 is 01001
Time Complexity: O(n)
Auxiliary Space: O(n)
Here, n is length of the binary string.
Binary to Gray using Bitwise Operators
C++
#include <bits/stdc++.h>
using namespace std;
int greyConverter( int n) { return n ^ (n >> 1); }
int main()
{
int n = 3;
cout << greyConverter(n) << endl;
n = 9;
cout << greyConverter(n) << endl;
return 0;
}
|
Java
public class Main {
public static int greyConverter( int n)
{
return n ^ (n >> 1 );
}
public static void main(String[] args)
{
int n = 3 ;
System.out.println(greyConverter(n));
n = 9 ;
System.out.println(greyConverter(n));
}
}
|
Python3
def greyConverter(n):
return n ^ (n >> 1 )
n = 3
print (greyConverter(n))
n = 9
print (greyConverter(n))
|
C#
using System;
class GFG {
static int greyConverter( int n) { return n ^ (n >> 1); }
public static void Main( string [] args)
{
int n = 3;
Console.WriteLine(greyConverter(n));
n = 9;
Console.WriteLine(greyConverter(n));
}
}
|
Javascript
<script>
function greyConverter(n)
{
return n ^ (n >> 1);
}
let n = 3;
document.write(greyConverter(n) + "</br>" );
n = 9;
document.write(greyConverter(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Gray to Binary using Bitwise Operators
C++
#include <bits/stdc++.h>
using namespace std;
int binaryConverter( int n)
{
int res = n;
while (n > 0)
{
n >>= 1;
res ^= n;
}
return res;
}
int main()
{
int n = 4;
cout << binaryConverter(n) << endl;
return 0;
}
|
Java
public class Main {
public static int binaryConverter( int n)
{
int res = n;
while (n > 0 ) {
n >>= 1 ;
res ^= n;
}
return res;
}
public static void main(String[] args)
{
int n = 4 ;
System.out.println(binaryConverter(n));
}
}
|
Python3
def binaryConverter(n):
res = n
while n > 0 :
n >> = 1
res ^ = n
return res
n = 4
print (binaryConverter(n))
|
C#
using System;
public class GFG {
static int binaryConverter( int n)
{
int res = n;
while (n > 0) {
n >>= 1;
res ^= n;
}
return res;
}
static public void Main()
{
int n = 4;
Console.WriteLine(binaryConverter(n));
}
}
|
Javascript
<script>
function binaryConverter(n)
{
let res = n;
while (n > 0)
{
n >>= 1;
res ^= n;
}
return res;
}
let n = 4;
document.write(binaryConverter(n));
</script>
|
Time Complexity: O(log(n))
Auxiliary Space: O(1)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Jul, 2022
Like Article
Save Article