Gray to Binary equivalent | Practice | GeeksforGeeks

Given N in Gray Code, find its binary equivalent. Return the decimal representation of the binary equivalent.
Example 1:
Input: N = 4
Output: 7
Explanation:
Given 4 representing gray code 110.
Binary equivalent of gray code 110 is 100.
Return 7 representing gray code 100.

Example 2:
Input: N = 15
Output: 10
Explanation:
Given 15 representing gray code 1000.
Binary equivalent of gray code 1000 is 1111.
Return 10 representing gray code 1111 
ie binary 1010.
Example 3:
Input: N = 0
Output: 0
Explanation: 
Zero remains the same in all systems.

Your Task: 
You don't need to read input or print anything. Your task is to complete the function grayToBinary() which accepts an integer n as an input parameter and returns decimal of the binary equivalent of the given gray code. 

Expected Time Complexity: O(log N)
Expected Auxiliary Space: O(1)

Constraints:
0 <= N <= 108
0
anasuya26677Premium1 day ago
//Back-end complete function Template for Java

class Solution{

    // function to convert a given Gray equivalent n to Binary equivalent.
  
  public static int grayToBinary(int n) {

        int b=0;
     
   
        //We use a loop and Right shift n everytime until it becomes 0.
   
     for(n=n;n>0;n=n>>1)
         //We use XOR operation which stores the result of conversion in b.
            b^=n;
    
        
        //returning the Binary equivalent.
        return b;
   
 }


}
0
crawler2 weeks ago
class Solution{
    public:
    // function to convert a given Gray equivalent n to Binary equivalent.
    int grayToBinary(int n)
    {
        int result = 0;
        for(int i=30; i>= 0; i--){
            if(result == 0){
                if(n & (1 << i)){
                    result |= (1<<i);
                }
            }
            else{
                int significant = (result & (1 << (i+1)));
                significant >>= 1;
                if(n & (1 << i)){
                    significant ^= (1 << i);
                }
                result = result | significant;
            }
        }
        return result;
    }
};

0
sanapalavenkatesh20014 weeks ago
   int val=n<<1;              vector<int>v;             // v.push_back(val);       while(n)       {         if(n&1)            v.push_back(1);         else           v.push_back(0);         n >>=1;       }       reverse(v.begin(),v.end());              for(int i=1;i<v.size();i++)           {               v[i]=v[i-1]^v[i];           }       reverse(v.begin(),v.end());       int sumi=0;       for(int i=0;i<v.size();i++)           {               if(v[i]==1)                 {                     sumi +=pow(2,i);                  }           }       return sumi
+2
1ashishchauhan20021 month ago
 int t=0;       while(n){           t=t^n;           n=n>>1;       }             return t;
+2
singhisha7181 month ago
 int grayToBinary(int n)
    {
        int x=0;
        while(n){
            x=x^n;
            n=n>>1;
        }
       return x;
        
    }
0
raunakmishra12432 months ago
int grayToBinary(int n)
    {
        
        if(n==0)
        return 0;
        vector<int>bin;
        while(n)
        {
            bin.push_back(n%2);
            n=n/2;
            
            
            
        }
        reverse(bin.begin(),bin.end());
        for(int i=1;i<bin.size();i++)
        {
            
            bin[i]=bin[i]^bin[i-1];
            
        }
        int ans=0;
        int k=0;
        for(int i=bin.size()-1;i>=0;i--)
        {
            ans=ans+pow(2,k++)*bin[i];
            
            
        }
        return ans;
        
    }
+3
naitikrajyaguru3 months ago
// take xor of bit and privious bit will be result into binary to gray or reverse   int grayToBinary(int n)   {       vector <int> binary;       if(n==0){           return 0;       }       //binary       while(n){           binary.push_back(n%2);           n/=2;       }       reverse(binary.begin(), binary.end());       //xor       for(int i=1; i<=binary.size()-1; i++){           binary[i]= binary[i] ^ binary[i-1];                 }       int ans=0;       reverse(binary.begin(), binary.end());       //decimal       for(int i=0; i<=binary.size()-1; i++){           ans+= binary[i]*pow(2,i);       }       return ans;
0
chessnoobdj4 months ago
Easy C++
int grayToBinary(int num){
        num ^= num >> 16;
        num ^= num >>  8;
        num ^= num >>  4;
        num ^= num >>  2;
        num ^= num >>  1;
        return num;
    }
int grayToBinary(int num){
        int mask = num;
        while (mask) {          
            mask >>= 1;
            num   ^= mask;
        }
        return num;
    }
+2
codxzaheer4 months ago
Let b0, b1, b2 and b3 be the bits representing the binary numbers, where b0 is the LSB and b3 is the MSB, andLet g0, g1, g2 and g3 be the bits representing the gray code of the binary numbers, where g0 is the LSB and g3 is the MSB.
b0=g3^g2^g1^g0
b1=g3^g2^g1
b2=g3^g2
b3=g3
So, start a loop and keep doing Right Shift untill n becomes 0 and keep storing the result of XOR operation.
 // Your code here              int ans= 0;       while(n)       {           ans = ans ^ n;           n = n>>1;       }       return ans;
 
0
salmanmajeed245 months ago
 int ans=0;
        while(n){
            ans = ans ^ n;
            n = n>>1;
            
        }
        return ans;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.