Binary Code of a number is the representation of a number in Binary (base-2) number system. In Binary Number System, each number is expressed using only two literals (0 and 1). Each of these literals is called a bit. The binary number system is very useful in digital electronic circuits.
Gray Code of a number is the representation of a number using binary literals. But the difference between Binary code and Gray code is that in Gray code, every pair of consecutive numbers differ only by one bit. Gray codes are very useful for the implementation of K-Maps and error correction.
Example:
Binary -> 0100
Gray -> 0110
Binary -> 0101
Gray -> 0111
A Gray code can be converted into a Binary code and vice versa.
Binary to Gray Conversion:
A Binary code can be converted into its equivalent Gray code as:
- The MSB(Most Significant Bit) of the Binary code will be the MSB of the equivalent Gray code.
- All the remaining bits are obtained by performing the XOR operation on the bit at that position with the bit at the previous position in the binary string.
Example:
Binary -> 0011010101
0 XOR 0 XOR 1 XOR 1 XOR 0 XOR 1 XOR 0 XOR 1 XOR 0 XOR 1
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
Gray -> 0 0 1 0 1 1 1 1 1 1
Binary -> 0100110
Gray -> 0110101
Code 1:
We use ‘^’ to perform the Xor operation by converting the string to an integer value using Integer.parseInt() function and then performing xor n them.
Java
import java.io.*;
class GFG {
public static void toGray(String binary)
{
String gray = new String();
gray += binary.charAt( 0 );
for ( int i = 1 ; i < binary.length(); i++) {
gray += (Integer.parseInt(String.valueOf(
binary.charAt(i - 1 )))
^ Integer.parseInt(
String.valueOf(binary.charAt(i))));
}
System.out.println( "The gray code of " + binary
+ " is : " + gray);
}
public static void main(String[] args)
{
String binary = "0011010101" ;
toGray(binary);
}
}
|
Output
The gray code of 0011010101 is : 0010111111
Time complexity: O(n)
Auxiliary space: O(n)
Code 2:
We make a separate user-defined function named xOR(char a, char b) which returns us the xor of two numbers a and b.
Java
import java.io.*;
class GFG {
public static int xOR( char a, char b)
{
if (a != b)
return 1 ;
return 0 ;
}
public static void toGray(String binary)
{
String gray = new String();
gray += binary.charAt( 0 );
for ( int i = 1 ; i < binary.length(); i++) {
gray += xOR(binary.charAt(i - 1 ),
binary.charAt(i));
}
System.out.println( "The gray code of " + binary
+ " is : " + gray);
}
public static void main(String[] args)
{
String binary = "0011010101" ;
toGray(binary);
}
}
|
Output
The gray code of 0011010101 is : 0010111111
Time complexity: O(n) where n is length of input string of binary code.
Auxiliary space: O(n) because extra space for String gray is being used
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Sep, 2022
Like Article
Save Article