Skip to content
Related Articles

Related Articles

Improve Article

Java Program to Convert Binary Code into Gray Code Without Using Recursion

  • Last Updated : 15 Apr, 2021

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 Xor operation by converting the string to an integer value using Integer.parseInt() funtion and then performing xor n them.

Java




// Java program to demonstrate Binary
// to Gray conversion
 
import java.io.*;
 
class GFG {
 
    // converts the given binary string into its equivalent
    // gray code
    public static void toGray(String binary)
    {
        // a String variable to store the obtained gray
        // string.
        // the MSB of the gray code is the same as
        // the MSB of the binary string, i.e., binary[0]
        String gray = new String();
 
        gray += binary.charAt(0);
 
        
        for (int i = 1; i < binary.length(); i++)
        {
            // perform XOR on the previous bit and the
            // current bit of the binary string
         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)
    {
        // a String variable to store the given binary
        // string
        String binary = "0011010101";
 
        toGray(binary);
    }
}

 
 

Output
The gray code of 0011010101 is : 0010111111

 

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




// Java program to demonstrate Binary
// to Gray conversion
 
import java.io.*;
 
class GFG {
   
    // an auxiliary method to perform XOR on two given
    // literals
    public static int xOR(char a, char b)
    {
        // return 1 if both bits are not same
        if (a != b)
            return 1;
       
        // else return 0
        return 0;
    }
   
    // converts the given binary string into its equivalent
    // gray code
    public static void toGray(String binary)
    {
       
        String gray = new String();
       
        gray += binary.charAt(0);
       
        // for all the other bits, traverse through the
        // binary string
        for (int i = 1; i < binary.length(); i++)
        {
            // calling xOR() method on the previous bit and
            // the current bit of the binary string
            gray += xOR(binary.charAt(i - 1),
                        binary.charAt(i));
        }
       
        System.out.println("The gray code of " + binary
                           + " is : " + gray);
    }
 
    // Driver method
    public static void main(String[] args)
    {
        // a String variable to store the given binary
        // string
        String binary = "0011010101";
       
        toGray(binary);
       
    }
}

 
 

Output
The gray code of 0011010101 is : 0010111111

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :