Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-317

Gray Code Decode

Problem

Convert a Gray code value back to its binary value. In a Gray code, consecutive values differ by exactly one bit, and the binary value is recovered by repeatedly XOR-ing the code with itself shifted right.

One efficient method accumulates the XOR of every suffix of the Gray code bits. Assume g >= 0.

Hint

Accumulating XOR of the code shifted right one bit at a time undoes the Gray encoding.

Test cases

case 1
in: [0]
expected: 0
case 2
in: [1]
expected: 1
case 3
in: [3]
expected: 2
case 4
in: [6]
expected: 4
case 5
in: [4]
expected: 7
Starter codePython
def gray_decode(g):
    # Your code here
    pass

5 test cases · run your code against them in the editor