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

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

 `// Java program to demonstrate Binary` `// to Gray conversion`   `import` `java.io.*;`   `// Class` `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);` `    ``}`   `    ``//  Main 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`

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

 `// 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`

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!

Previous
Next