Java Program to Convert Binary Code into Gray Code Without Using Recursion
Last Updated :
22 Sep, 2022
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);
}
}
OutputThe 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);
}
}
OutputThe 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
Similar Reads
Java Program to Convert Binary Code Into Equivalent Gray Code Using Recursion Convert the Binary code of the Number into it's equivalent Gray's code using recursion. Binary is the default way to store numbers, but in many applications, binary numbers are not useful and a variation of binary is needed. Gray code has the property that two successive numbers differ in only one b
3 min read
Java Program to Convert Integer Values into Binary Given an integer in Java, your task is to write a Java program to convert this given integer into a binary number. Example: Input: = 45 Output: = 101101 Input: = 32 Output: = 100000 Integers: Integers are numbers whose base value is 10. The Integer or int data type is a 32-bit signed twoâs complemen
4 min read
Java Program to Convert Binary String to Decimal Using Wrapper Class Given a binary string as input, we need to write a program to convert the given binary string into its equivalent decimal number. Examples: Input : 1111 Output : 15 Input : 1001101 Output : 77 Input : 1101 Output : 13 The idea is to extract each character of a given binary string using charAt() and
2 min read
Java Program to Convert Binary to Hexadecimal The Hexadecimal number system as the name suggests comprises 16 entities. These 16 entities consist of 10 digits, 0-9 representing the first 10 numbers of the hexadecimal system as well. For the remaining 6 numbers, we use English alphabets ranging from A through F to represent the numbers 10 to 15.
6 min read
Print Binary Equivalent of an Integer using Recursion in Java Given an integer number as input, we need to write a program to convert the given Integer number into an equivalent binary number by using JAVA. BigInteger class Is used for the mathematical operation which involves very big integer calculations that are outside the limit of all available primitive
2 min read
Java Program to Convert Octal to Binary Given an Octal number as input, the task is to convert that number into its Binary equivalent number. Example: Input: Octal Number = 513 Output: Binary equivalent value is: 101001011 Explanation : Binary equivalent value of 5: 101 Binary equivalent value of 1: 001 Binary equivalent value of 3: 011Oc
5 min read
Java Program to Convert Binary to Octal A Binary (base 2) number is given, and our task is to convert it into an Octal (base 8) number. There are different ways to convert binary to decimal, which we will discuss in this article.Example of Binary to Octal Conversion:Input : 100100Output: 44Input : 1100001Output : 141A Binary Number System
5 min read
Java Program to Convert a Decimal Number to Binary Number using Stacks Java is high level, compiled as well as interpreted programming language. Stack is an abstract data type used in most of the programming languages and can be implemented using arrays or linked list. Stack data structure follows the principle of LIFO (Last In First Out) . Stack allows push, pop, peek
3 min read
Java Program to Convert a Decimal Number to Binary Number using Arrays as Stacks Given an Integer number convert into Binary Number using arrays as a stack. Example: Input : 10 Output: 1010 Input : 16 Output: 10000 Approach: Divide the number by 2 and store the remainder of the number in the array.Divide the number by 2.Repeat the process until the number becomes zero.Print the
1 min read
Java Program to Add Two Binary Strings When two binary strings are added, then the sum returned is also a binary string. Example: Input : x = "10", y = "01" Output: "11"Input : x = "110", y = "011" Output: "1001" Explanation: 110 + 011 =1001Approach 1: Here, we need to start adding from the right side and when the sum returned is more th
3 min read