Given the Binary code of a number as a decimal number, we need to convert this into its equivalent Gray Code. Assume that the binary number is in the range of integers. For the larger value, we can take a binary number as string.
In gray code, only one bit is changed in 2 consecutive numbers.
Examples:
Input: 1001
Output: 1101
Explanation: 1001 -> 1101 -> 1101 -> 1101
Input: 11
Output: 10
Explanation: 11 -> 10
Approach:
The idea is to check whether the last bit and second last bit are same or not, if it is same then move ahead otherwise add 1.
Follow the steps to solve the given problem:
- binary_to_grey(n)
- if n == 0
- else if last two bits are opposite to each other
- grey = 1 + 10 * binary_to_gray(n/10))
- else if last two bits are same
- grey = 10 * binary_to_gray(n/10))
Below is the implementation of the above approach :
CPP
#include <bits/stdc++.h>
using namespace std;
int binary_to_gray( int n)
{
if (!n)
return 0;
int a = n % 10;
int b = (n / 10) % 10;
if ((a && !b) || (!a && b))
return (1 + 10 * binary_to_gray(n / 10));
return (10 * binary_to_gray(n / 10));
}
int main()
{
int binary_number = 1011101;
printf ( "%d" , binary_to_gray(binary_number));
return 0;
}
|
Java
import static java.lang.StrictMath.pow;
import java.util.Scanner;
class bin_gray {
int binary_to_gray( int n, int i)
{
int a, b;
int result = 0 ;
if (n != 0 ) {
a = n % 10 ;
n = n / 10 ;
b = n % 10 ;
if ((a & ~b) == 1 || (~a & b) == 1 ) {
result = ( int )(result + pow( 10 , i));
}
return binary_to_gray(n, ++i) + result;
}
return 0 ;
}
public static void main(String[] args)
{
int binary_number;
int result = 0 ;
binary_number = 1011101 ;
bin_gray obj = new bin_gray();
result = obj.binary_to_gray(binary_number, 0 );
System.out.print(result);
}
}
|
Python3
def binary_to_gray(n):
if not (n):
return 0
a = n % 10
b = int (n / 10 ) % 10
if (a and not (b)) or ( not (a) and b):
return ( 1 + 10 * binary_to_gray( int (n / 10 )))
return ( 10 * binary_to_gray( int (n / 10 )))
binary_number = 1011101
print (binary_to_gray(binary_number), end = '')
|
C#
using System;
class GFG {
static int binary_to_gray( int n, int i)
{
int a, b;
int result = 0;
if (n != 0) {
a = n % 10;
n = n / 10;
b = n % 10;
if ((a & ~b) == 1 || (~a & b) == 1) {
result = ( int )(result + Math.Pow(10, i));
}
return binary_to_gray(n, ++i) + result;
}
return 0;
}
public static void Main()
{
int binary_number;
binary_number = 1011101;
Console.WriteLine(binary_to_gray(binary_number, 0));
}
}
|
PHP
<?php
function binary_to_gray( $n )
{
if (! $n )
return 0;
$a = $n % 10;
$b = ( $n / 10) % 10;
if (( $a && ! $b ) || (! $a && $b ))
return (1 + 10 * binary_to_gray( $n / 10));
return (10 * binary_to_gray( $n / 10));
}
$binary_number = 1011101;
echo binary_to_gray( $binary_number );
?>
|
Javascript
<script>
function binary_to_gray(n, i)
{
let a, b;
let result = 0;
if (n != 0)
{
a = n % 10;
n = n / 10;
b = n % 10;
if ((a & ~ b) == 1 || (~ a & b) == 1)
{
result = (result + Math.pow(10,i));
}
return binary_to_gray(n, ++i) + result;
}
return 0;
}
let binary_number;
let result = 0;
binary_number = 1011101;
result = binary_to_gray(binary_number,0);
document.write(result);
</script>
|
Time Complexity: O(logN), Traverse through all the digits, as there are logN bits.
Auxiliary Space: O(1)