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 Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read