Gray to Binary Conversion

Last Updated : 29 Jun, 2026

Given a non-negative integer n, whose binary representation represents a Gray code, convert this Gray code to its equivalent binary number and return the decimal value of that binary number.

Examples:

Input: n = 4
Output: 7
Explanation: The binary representation of 4 is 100. Treating 100 as a Gray code, its corresponding binary representation is 111, whose decimal value is 7.

Input: n = 7
Output: 5
Explanation: The binary representation of 7 is 111. Treating 111 as a Gray code, its corresponding binary representation is 101, whose decimal value is 5.

Try It Yourself
redirect icon

The following table shows the conversion of Gray code values to their corresponding binary values:

Decimal Value of Gray CodeGray CodeBinary EquivalentDecimal Value of Binary Equivalent
00000000
10010011
20100113
30110102
41001117
51011106
61101004
71111015

Using Bit Manipulation - O(log(n)) Time and O(1) Space

The most significant bit of a binary number is the same as the most significant bit of its Gray code. Every subsequent binary bit can be obtained by XORing the previous binary bit with the corresponding Gray code bit.

Based on this property, the original binary number can be reconstructed by taking the cumulative XOR of the Gray code and all of its right-shifted values:

Binary = Gray ^ (Gray >> 1) ^ (Gray >> 2) ^ ...

We initialize a variable b as 0. Then, we repeatedly XOR b with the current value of n and right shift n by one bit. This process continues until n becomes 0. After all bits have been processed, b stores the decimal value of the equivalent binary number.

Consider n = 7.

The binary representation of 7 is 111, which is treated as a Gray code.

  • b = 0, b = b ^ 111 = 111
  • n = 111 >> 1 = 011, b = b ^ 011 = 100
  • n = 011 >> 1 = 001, b = b ^ 001 = 101
  • n = 001 >> 1 = 000 stop here because n=0.

The resulting binary representation is 101, whose decimal value is 5.

C++
#include <bits/stdc++.h>
using namespace std;

int grayToBinary(int n) {
    int b = 0;

    // Traverse all bits of Gray code
    while (n > 0) {

        // Build binary number step by step using XOR
        b ^= n;

        // Move to next bit
        n = n >> 1;
    }

    return b;
}

int main() {
    int n = 7;

    cout << grayToBinary(n);

    return 0;
}
C
#include <stdio.h>

int grayToBinary(int n) {
    int b = 0;

    // Traverse all bits of Gray code
    while (n > 0) {

        // Build binary number step by step using XOR
        b ^= n;

        // Move to next bit
        n = n >> 1;
    }

    return b;
}

int main() {
    int n = 7;

    printf("%d", grayToBinary(n));

    return 0;
}
Java
class GFG {

    static int grayToBinary(int n) {
        int b = 0;

        // Traverse all bits of Gray code
        while (n > 0) {

            // Build binary number step by step using XOR
            b ^= n;

            // Move to next bit
            n = n >> 1;
        }

        return b;
    }

    public static void main(String[] args) {
        int n = 7;

        System.out.print(grayToBinary(n));
    }
}
Python
def grayToBinary(n):
    b = 0

    # Traverse all bits of Gray code
    while n > 0:

        # Build binary number step by step using XOR
        b ^= n

        # Move to next bit
        n = n >> 1

    return b


if __name__ == "__main__":
    n = 7

    print(grayToBinary(n))
C#
using System;

class GFG
{
    static int grayToBinary(int n)
    {
        int b = 0;

        // Traverse all bits of Gray code
        while (n > 0)
        {

            // Build binary number step by step using XOR
            b ^= n;

            // Move to next bit
            n = n >> 1;
        }

        return b;
    }

    static void Main()
    {
        int n = 7;

        Console.Write(grayToBinary(n));
    }
}
JavaScript
function grayToBinary(n) {
    let b = 0;

    // Traverse all bits of Gray code
    while (n > 0) {

        // Build binary number step by step using XOR
        b ^= n;

        // Move to next bit
        n = n >> 1;
    }

    return b;
}

// Driver code

    let n = 7;

    console.log(grayToBinary(n));

Output
5
Comment