Conversion from binary to gray code
In digital electronics and telecommunications, binary and gray codes are commonly used for representing data. The gray code, also known as reflected binary code, is a binary numeral system where two successive values differ by only one bit. It is particularly useful in applications where noise or errors can cause issues with traditional binary representations.
Problem Statement
The problem is to convert a given binary number into its corresponding gray code representation. The binary number is a base-2 numeral system, consisting of only 0s and 1s. The gray code is obtained by making sure that two consecutive values differ by only one bit. For example, the binary number "01110101" should be converted to its gray code equivalent, which is "01001111".
Explanation with an Example
Let's understand the conversion process using the binary number "01110101" step-by-step.
- Start with the given binary number: 01110101
- Write down the most significant bit as it is: 0 (first bit)
- Compare the current bit with the previous bit:
- If they are the same (0 and 1 or 1 and 0), write 0.
- If they are different (0 and 0 or 1 and 1), write 1.
- Repeat this process for all remaining bits.
Step-by-step conversion:
- 0 (0) - First bit remains the same.
- 1 (1) - Compare 1 with the previous bit 0. They are different, so write 1.
- 1 (1) - Compare 1 with the previous bit 1. They are the same, so write 0.
- 1 (1) - Compare 1 with the previous bit 1. They are the same, so write 0.
- 0 (0) - Compare 0 with the previous bit 1. They are different, so write 1.
- 1 (1) - Compare 1 with the previous bit 0. They are different, so write 1.
- 0 (0) - Compare 0 with the previous bit 1. They are different, so write 1.
- 1 (1) - Compare 1 with the previous bit 0. They are different, so write 1.
So, the gray code representation of "01110101" is "01001111".
Pseudocode and Algorithm
Here's the pseudocode for converting a binary number to its gray code equivalent:
Function binaryToGray(binary): Initialize n as the length of binary Create an array called gray of size n + 1 Set gray[0] = binary[0] Loop from i = 1 to n: If binary[i] == binary[i - 1]: Set gray[i] = '0' Else: Set gray[i] = '1' Set gray[n] = '\0' Display the calculated gray code
Explanation of the algorithm:
- We start by initializing the length of the binary number and creating an array to store the gray code.
- Next, we set the first element of the gray code to be the same as the first element of the binary number.
- We then iterate through the rest of the binary number, comparing each bit with its previous bit.
- If they are the same, we add '0' to the gray code array, indicating that the corresponding gray code bit should be the same as the binary bit.
- If they are different, we add '1' to the gray code array, indicating that the corresponding gray code bit should be different from the binary bit.
- Finally, we terminate the gray code array with a null character and display the calculated gray code.
Code Solution
// C Program
// Conversion from Binary to gray code
#include <stdio.h>
#include <string.h>
// Convert given binary number to gray code
void binaryToGray(const char *binary)
{
// Get the length of given number
int n = strlen(binary);
// Used to collect result
char gray[n + 1];
// Set first most significant bit
gray[0] = binary[0];
// Execute loop through by length
for (int i = 1; i < n; ++i)
{
if (binary[i] == binary[i - 1])
{
// When two consecutive binary bits are some
gray[i] = '0';
}
else
{
// When two consecutive binary bits are different
gray[i] = '1';
}
}
gray[n] = '\0';
// Display calculated result
printf(" Binary : %s ", binary);
printf("\n Gray : %s \n\n", gray);
}
int main()
{
binaryToGray("01110101");
binaryToGray("1010");
return 0;
}
Output
Binary : 01110101
Gray : 01001111
Binary : 1010
Gray : 1111
/*
Java Program
Conversion from Binary to gray code
*/
public class Conversion
{
// Convert given binary number to gray code
public void binaryToGray(String binary)
{
// Get the length of given number
int n = binary.length();
// Used to collect result
String gray = "";
// Set first most significant bit
gray = ""+ binary.charAt(0);
// Execute loop through by length
for (int i = 1; i < n; ++i)
{
if (binary.charAt(i) == binary.charAt(i-1))
{
// When two consecutive binary bits are some
gray += "0";
}
else
{
// When two consecutive binary bits are different
gray += "1";
}
}
// Display calculated result
System.out.print(" Binary : " + binary);
System.out.print("\n Gray : " + gray + " \n\n");
}
public static void main(String[] args)
{
Conversion task = new Conversion();
task.binaryToGray("01110101");
task.binaryToGray("1010");
}
}
Output
Binary : 01110101
Gray : 01001111
Binary : 1010
Gray : 1111
// Include header file
#include <iostream>
#include <string.h>
using namespace std;
/*
C++ Program
Conversion from Binary to gray code
*/
class Conversion
{
public:
// Convert given binary number to gray code
void binaryToGray(string binary)
{
// Get the length of given number
int n = binary.size();
// Used to collect result
string gray = "";
// Set first most significant bit
gray = binary[0];
// Execute loop through by length
for (int i = 1; i < n; ++i)
{
if (binary[i] == binary[i - 1])
{
// When two consecutive binary bits are some
gray += "0";
}
else
{
// When two consecutive binary bits are different
gray += "1";
}
}
// Display calculated result
cout << " Binary : " << binary;
cout << "\n Gray : " << gray << " \n\n";
}
};
int main()
{
Conversion task = Conversion();
task.binaryToGray("01110101");
task.binaryToGray("1010");
return 0;
}
Output
Binary : 01110101
Gray : 01001111
Binary : 1010
Gray : 1111
// Include namespace system
using System;
/*
C# Program
Conversion from Binary to gray code
*/
public class Conversion
{
// Convert given binary number to gray code
public void binaryToGray(String binary)
{
// Get the length of given number
int n = binary.Length;
// Used to collect result
String gray = "";
// Set first most significant bit
gray = "" + binary[0];
// Execute loop through by length
for (int i = 1; i < n; ++i)
{
if (binary[i] == binary[i - 1])
{
// When two consecutive binary bits are some
gray += "0";
}
else
{
// When two consecutive binary bits are different
gray += "1";
}
}
// Display calculated result
Console.Write(" Binary : " + binary);
Console.Write("\n Gray : " + gray + " \n\n");
}
public static void Main(String[] args)
{
Conversion task = new Conversion();
task.binaryToGray("01110101");
task.binaryToGray("1010");
}
}
Output
Binary : 01110101
Gray : 01001111
Binary : 1010
Gray : 1111
<?php
/*
Php Program
Conversion from Binary to gray code
*/
class Conversion
{
// Convert given binary number to gray code
public function binaryToGray($binary)
{
// Get the length of given number
$n = strlen($binary);
// Used to collect result
$gray = "";
// Set first most significant bit
$gray = "".$binary[0];
// Execute loop through by length
for ($i = 1; $i < $n; ++$i)
{
if ($binary[$i] == $binary[$i - 1])
{
// When two consecutive binary bits are some
$gray .= "0";
}
else
{
// When two consecutive binary bits are different
$gray .= "1";
}
}
// Display calculated result
echo " Binary : ". $binary;
echo "\n Gray : ". $gray ." \n\n";
}
}
function main()
{
$task = new Conversion();
$task->binaryToGray("01110101");
$task->binaryToGray("1010");
}
main();
Output
Binary : 01110101
Gray : 01001111
Binary : 1010
Gray : 1111
/*
Node Js Program
Conversion from Binary to gray code
*/
class Conversion
{
// Convert given binary number to gray code
binaryToGray(binary)
{
// Get the length of given number
var n = binary.length;
// Used to collect result
var gray = "";
// Set first most significant bit
gray = binary[0];
// Execute loop through by length
for (var i = 1; i < n; ++i)
{
if (binary[i] == binary[i - 1])
{
// When two consecutive binary bits are some
gray += "0";
}
else
{
// When two consecutive binary bits are different
gray += "1";
}
}
// Display calculated result
process.stdout.write(" Binary : " + binary);
process.stdout.write("\n Gray : " + gray + " \n\n");
}
}
function main()
{
var task = new Conversion();
task.binaryToGray("01110101");
task.binaryToGray("1010");
}
main();
Output
Binary : 01110101
Gray : 01001111
Binary : 1010
Gray : 1111
# Python 3 Program
# Conversion from Binary to gray code
class Conversion :
# Convert given binary number to gray code
def binaryToGray(self, binary) :
# Get the length of given number
n = len(binary)
# Used to collect result
gray = ""
# Set first most significant bit
gray = binary[0]
i = 1
# Execute loop through by length
while (i < n) :
if (binary[i] == binary[i - 1]) :
# When two consecutive binary bits are some
gray += "0"
else :
# When two consecutive binary bits are different
gray += "1"
i += 1
# Display calculated result
print(" Binary : ", binary, end = "")
print("\n Gray : ", gray ," \n")
def main() :
task = Conversion()
task.binaryToGray("01110101")
task.binaryToGray("1010")
if __name__ == "__main__": main()
Output
Binary : 01110101
Gray : 01001111
Binary : 1010
Gray : 1111
# Ruby Program
# Conversion from Binary to gray code
class Conversion
# Convert given binary number to gray code
def binaryToGray(binary)
# Get the length of given number
n = binary.length()
# Used to collect result
gray = ""
# Set first most significant bit
gray = binary[0]
i = 1
# Execute loop through by length
while (i < n)
if (binary[i] == binary[i - 1])
# When two consecutive binary bits are some
gray += "0"
else
# When two consecutive binary bits are different
gray += "1"
end
i += 1
end
# Display calculated result
print(" Binary : ", binary)
print("\n Gray : ", gray ," \n\n")
end
end
def main()
task = Conversion.new()
task.binaryToGray("01110101")
task.binaryToGray("1010")
end
main()
Output
Binary : 01110101
Gray : 01001111
Binary : 1010
Gray : 1111
/*
Scala Program
Conversion from Binary to gray code
*/
class Conversion
{
// Convert given binary number to gray code
def binaryToGray(binary: String): Unit = {
// Get the length of given number
var n: Int = binary.length();
// Used to collect result
var gray: String = "";
// Set first most significant bit
gray = "" + binary(0);
var i: Int = 1;
// Execute loop through by length
while (i < n)
{
if (binary(i) == binary(i - 1))
{
// When two consecutive binary bits are some
gray += "0";
}
else
{
// When two consecutive binary bits are different
gray += "1";
}
i += 1;
}
// Display calculated result
print(" Binary : " + binary);
print("\n Gray : " + gray + " \n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Conversion = new Conversion();
task.binaryToGray("01110101");
task.binaryToGray("1010");
}
}
Output
Binary : 01110101
Gray : 01001111
Binary : 1010
Gray : 1111
/*
Swift 4 Program
Conversion from Binary to gray code
*/
class Conversion
{
// Convert given binary number to gray code
func binaryToGray(_ num: String)
{
// Get the length of given number
let n: Int = num.count;
var binary = Array(num);
// Used to collect result
var gray: String = "";
// Set first most significant bit
gray = String(binary[0]);
var i: Int = 1;
// Execute loop through by length
while (i < n)
{
if (binary[i] == binary[i - 1])
{
// When two consecutive binary bits are some
gray += "0";
}
else
{
// When two consecutive binary bits are different
gray += "1";
}
i += 1;
}
// Display calculated result
print(" Binary : ", num, terminator: "");
print("\n Gray : ", gray ," \n");
}
}
func main()
{
let task: Conversion = Conversion();
task.binaryToGray("01110101");
task.binaryToGray("1010");
}
main();
Output
Binary : 01110101
Gray : 01001111
Binary : 1010
Gray : 1111
/*
Kotlin Program
Conversion from Binary to gray code
*/
class Conversion
{
// Convert given binary number to gray code
fun binaryToGray(binary: String): Unit
{
// Get the length of given number
var n: Int = binary.count();
// Used to collect result
// Set first most significant bit
var gray: String = "" + binary[0];
var i: Int = 1;
// Execute loop through by length
while (i < n)
{
if (binary[i] == binary[i - 1])
{
// When two consecutive binary bits are some
gray += "0";
}
else
{
// When two consecutive binary bits are different
gray += "1";
}
i += 1;
}
// Display calculated result
print(" Binary : " + binary);
print("\n Gray : " + gray + " \n\n");
}
}
fun main(args: Array < String > ): Unit
{
var task: Conversion = Conversion();
task.binaryToGray("01110101");
task.binaryToGray("1010");
}
Output
Binary : 01110101
Gray : 01001111
Binary : 1010
Gray : 1111
Resultant Output Explanation
Let's now analyze the output of the provided code for two binary numbers: "01110101" and "1010".
- Binary: 01110101
The given binary number is "01110101". Using the conversion algorithm, we get the gray code "01001111". - Binary: 1010
The given binary number is "1010". Using the conversion algorithm, we get the gray code "1111".
Time Complexity of the Code
The time complexity of the provided code is O(n), where n is the length of the given binary number. This is because the code iterates through each bit of the binary number once to calculate the gray code. The operations within the loop, such as comparisons and assignments, take constant time, so they do not significantly impact the overall time complexity.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment