Skip to main content

Conversion from gray to binary code

Conversion from gray to binary code refers to the process of converting a number that is represented in gray code, which is a binary numeral system where two consecutive values differ in only one bit, to its equivalent binary code representation.

The conversion process involves examining each bit of the gray code and using a specific algorithm to calculate the corresponding bit of the binary code. The algorithm varies depending on the number of bits in the gray code and the specific encoding scheme used.

To summarize the steps involved in converting gray code to binary code:

  • The first bit of the binary code is set to be the same as the first bit of the gray code.
  • For each subsequent bit, the binary value is set to 1 if the corresponding gray code bit is different from the previous binary code bit, and 0 otherwise.
  • Repeat this process until all bits in the gray code have been converted to binary.

For example, to convert the gray code number 1011 to its binary code equivalent, we can use the above steps as follows:

  • The first bit of the binary code is set to be 1, which is the same as the first bit of the gray code.
  • For the second bit, the gray code bit is 0, which is different from the previous binary code bit of 1, so the second bit of the binary code is set to 1, resulting in the binary code so far being 11.
  • For the third bit, the gray code bit is 1, which is different from the previous binary code bit of 0, so the third bit of the binary code is set to 1, resulting in the binary code so far being 110.
  • For the fourth bit, the gray code bit is 1, which is different from the previous binary code bit of 1, so the fourth bit of the binary code is set to 0, resulting in the final binary code being 1101.

Therefore, the binary equivalent of the gray code number 1011 is 1101.

Program Solution

/*
  Java Program
  Conversion from gray to binary code
*/
public class Conversion
{

    // Convert given gray code to binary number
    public void grayToBinary(String gray)
    {
        // Get the length of given number
        int n = gray.length();
        // Used to collect result
        String binary = "";
        // Set first most significant bit
        binary = ""+gray.charAt(0);
        // Execute loop through by length
        for (int i = 1; i < n; ++i)
        {
            if (gray.charAt(i)== '0')
            {
            
                binary += binary.charAt(i-1);
            }
            else if(binary.charAt(i-1)=='0')
            {
              
                binary += "1";
            }
            else
            {
                binary += "0";
            }
        }
    
        // Display calculated result  
        System.out.print(" Gray : " + gray );
        System.out.print("\n Binary : " + binary + " \n\n");
     
    }
    public static void main(String[] args)
    {
        Conversion task = new Conversion();

        task.grayToBinary("01001111");
        task.grayToBinary("1111");
    
    }
}

Output

 Gray : 01001111
 Binary : 01110101

 Gray : 1111
 Binary : 1010
// C Program
// Conversion from gray to binary code
#include <stdio.h>

#include <string.h>

// Convert given gray code to binary number
void grayToBinary(const char *gray)
{
	// Get the length of given gray code 
	int n = strlen(gray);
	// Used to collect result
	char binary[n + 1];
	// Set first most significant bit
	binary[0] = gray[0];
	// Execute loop through by length
	for (int i = 1; i < n; ++i)
	{
		if (gray[i] == '0')
		{
			binary[i] = binary[i - 1];
		}
		else if (binary[i - 1] == '0')
		{
			binary[i] = '1';
		}
		else
		{
			binary[i] = '0';
		}
	}
	binary[n] = '\0';
	// Display calculated result  
	printf(" Gray : %s", gray);
	printf("\n Binary : %s\n\n", binary);
}
int main()
{
	// Test Case
	grayToBinary("01001111");
	grayToBinary("1111");
	return 0;
}

Output

 Gray : 01001111
 Binary : 01110101

 Gray : 1111
 Binary : 1010
// Include header file
#include <iostream>
#include <string.h>

using namespace std;
/*
  C++ Program
  Conversion from gray to binary code
*/
class Conversion
{
	public:
		// Convert given gray code to binary number
		void grayToBinary(string gray)
		{
			// Get the length of given number
			int n = gray.size();
			// Used to collect result
			string binary = "";
			// Set first most significant bit
			binary = gray[0];
			// Execute loop through by length
			for (int i = 1; i < n; ++i)
			{
				if (gray[i] == '0')
				{
					binary += binary[i - 1];
				}
				else if (binary[i - 1] == '0')
				{
					binary += "1";
				}
				else
				{
					binary += "0";
				}
			}
			// Display calculated result
			cout << " Gray : " << gray;
			cout << "\n Binary : " << binary << " \n\n";
		}
};
int main()
{
	Conversion task = Conversion();
	task.grayToBinary("01001111");
	task.grayToBinary("1111");
	return 0;
}

Output

 Gray : 01001111
 Binary : 01110101

 Gray : 1111
 Binary : 1010
// Include namespace system
using System;
/*
  C# Program
  Conversion from gray to binary code
*/
public class Conversion
{
	// Convert given gray code to binary number
	public void grayToBinary(String gray)
	{
		// Get the length of given number
		int n = gray.Length;
		// Used to collect result
		String binary = "";
		// Set first most significant bit
		binary = "" + gray[0];
		// Execute loop through by length
		for (int i = 1; i < n; ++i)
		{
			if (gray[i] == '0')
			{
				binary += binary[i - 1];
			}
			else if (binary[i - 1] == '0')
			{
				binary += "1";
			}
			else
			{
				binary += "0";
			}
		}
		// Display calculated result
		Console.Write(" Gray : " + gray);
		Console.Write("\n Binary : " + binary + " \n\n");
	}
	public static void Main(String[] args)
	{
		Conversion task = new Conversion();
		task.grayToBinary("01001111");
		task.grayToBinary("1111");
	}
}

Output

 Gray : 01001111
 Binary : 01110101

 Gray : 1111
 Binary : 1010
<?php
/*
  Php Program
  Conversion from gray to binary code
*/
class Conversion
{
	// Convert given gray code to binary number
	public	function grayToBinary($gray)
	{
		// Get the length of given number
		$n = strlen($gray);
		// Used to collect result
		$binary = "";
		// Set first most significant bit
		$binary = "". $gray[0];
		// Execute loop through by length
		for ($i = 1; $i < $n; ++$i)
		{
			if ($gray[$i] == '0')
			{
				$binary .= $binary[$i - 1];
			}
			else if ($binary[$i - 1] == '0')
			{
				$binary .= "1";
			}
			else
			{
				$binary .= "0";
			}
		}
		// Display calculated result
		echo " Gray : ". $gray;
		echo "\n Binary : ". $binary ." \n\n";
	}
}

function main()
{
	$task = new Conversion();
	$task->grayToBinary("01001111");
	$task->grayToBinary("1111");
}
main();

Output

 Gray : 01001111
 Binary : 01110101

 Gray : 1111
 Binary : 1010
/*
  Node Js Program
  Conversion from gray to binary code
*/
class Conversion
{
	// Convert given gray code to binary number
	grayToBinary(gray)
	{
		// Get the length of given number
		var n = gray.length;
		// Used to collect result
		var binary = "";
		// Set first most significant bit
		binary = "" + gray[0];
		// Execute loop through by length
		for (var i = 1; i < n; ++i)
		{
			if (gray[i] == '0')
			{
				binary += binary[i - 1];
			}
			else if (binary[i - 1] == '0')
			{
				binary += "1";
			}
			else
			{
				binary += "0";
			}
		}
		// Display calculated result
		process.stdout.write(" Gray : " + gray);
		process.stdout.write("\n Binary : " + binary + " \n\n");
	}
}

function main()
{
	var task = new Conversion();
	task.grayToBinary("01001111");
	task.grayToBinary("1111");
}
main();

Output

 Gray : 01001111
 Binary : 01110101

 Gray : 1111
 Binary : 1010
#   Python 3 Program
#   Conversion from gray to binary code

class Conversion :
	#  Convert given gray code to binary number
	def grayToBinary(self, gray) :
		#  Get the length of given number
		n = len(gray)
		#  Used to collect result
		binary = ""
		#  Set first most significant bit
		binary = gray[0]
		i = 1
		#  Execute loop through by length
		while (i < n) :
			if (gray[i] == '0') :
				binary += binary[i - 1]
			
			elif(binary[i - 1] == '0') :
				binary += "1"
			else :
				binary += "0"
			
			i += 1
		
		#  Display calculated result
		print(" Gray : ", gray, end = "")
		print("\n Binary : ", binary ," \n")
	

def main() :
	task = Conversion()
	task.grayToBinary("01001111")
	task.grayToBinary("1111")

if __name__ == "__main__": main()

Output

 Gray :  01001111
 Binary :  01110101

 Gray :  1111
 Binary :  1010
#   Ruby Program
#   Conversion from gray to binary code

class Conversion 
	#  Convert given gray code to binary number
	def grayToBinary(gray) 
		#  Get the length of given number
		n = gray.length()
		#  Used to collect result
		binary = ""
		#  Set first most significant bit
		binary =  gray[0]
		i = 1
		#  Execute loop through by length
		while (i < n) 
			if (gray[i] == '0') 
				binary += binary[i - 1]
			elsif(binary[i - 1] == '0') 
				binary += "1"
			else 
				binary += "0"
			end

			i += 1
		end

		#  Display calculated result
		print(" Gray : ", gray)
		print("\n Binary : ", binary ," \n\n")
	end

end

def main() 
	task = Conversion.new()
	task.grayToBinary("01001111")
	task.grayToBinary("1111")
end

main()

Output

 Gray : 01001111
 Binary : 01110101 

 Gray : 1111
 Binary : 1010 

/*
  Scala Program
  Conversion from gray to binary code
*/
class Conversion
{
	// Convert given gray code to binary number
	def grayToBinary(gray: String): Unit = {
		// Get the length of given number
		var n: Int = gray.length();
		// Used to collect result
		var binary: String = "";
		// Set first most significant bit
		binary = "" + gray(0);
		var i: Int = 1;
		// Execute loop through by length
		while (i < n)
		{
			if (gray(i) == '0')
			{
				binary += binary(i - 1);
			}
			else if (binary(i - 1) == '0')
			{
				binary += "1";
			}
			else
			{
				binary += "0";
			}
			i += 1;
		}
		// Display calculated result
		print(" Gray : " + gray);
		print("\n Binary : " + binary + " \n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Conversion = new Conversion();
		task.grayToBinary("01001111");
		task.grayToBinary("1111");
	}
}

Output

 Gray : 01001111
 Binary : 01110101

 Gray : 1111
 Binary : 1010
/*
  Swift 4 Program
  Conversion from gray to binary code
*/
class Conversion
{
	// Convert given gray code to binary number
	func grayToBinary(_ gray: String)
	{
		// Get the length of given number
		let n: Int = gray.count;
		// Used to collect result
		var binary: String = "";
		// Set first most significant bit
		binary = String(Array(gray)[0]);
		var i: Int = 1;
		// Execute loop through by length
		while (i < n)
		{
			if (Array(gray)[i] == "0")
			{
				binary += String(Array(binary)[i-1]);
			}
			else if (Array(binary)[i-1] == "0")
			{
				binary += "1";
			}
			else
			{
				binary += "0";
			}
			i += 1;
		}
		// Display calculated result
		print(" Gray : ", gray, terminator: "");
		print("\n Binary : ", binary ," \n");
	}
}
func main()
{
	let task: Conversion = Conversion();
	task.grayToBinary("01001111");
	task.grayToBinary("1111");
}
main();

Output

 Gray :  01001111
 Binary :  01110101

 Gray :  1111
 Binary :  1010
/*
  Kotlin Program
  Conversion from gray to binary code
*/
class Conversion
{
	// Convert given gray code to binary number
	fun grayToBinary(gray: String): Unit
	{
		// Get the length of given number
		var n: Int = gray.count();
		// Used to collect result
      	// Set first most significant bit
		var binary: String = "" + gray[0];
		var i: Int = 1;
		// Execute loop through by length
		while (i < n)
		{
			if (gray[i] == '0')
			{
				binary += binary[i - 1];
			}
			else if (binary[i - 1] == '0')
			{
				binary += "1";
			}
			else
			{
				binary += "0";
			}
			i += 1;
		}
		// Display calculated result
		print(" Gray : " + gray);
		print("\n Binary : " + binary + " \n\n");
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Conversion = Conversion();
	task.grayToBinary("01001111");
	task.grayToBinary("1111");
}

Output

 Gray : 01001111
 Binary : 01110101

 Gray : 1111
 Binary : 1010




Comment

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