FunctionRacerProblemsRace

Gray Position

EasyBitsPython, JavaScript, TypeScript, Java, C++

In the reflected Gray code, the value at position i is i ^ (i >> 1), where ^ is XOR and >> is a right shift. You are given a Gray code value g. Return its position: the non-negative integer i with i ^ (i >> 1) == g. Exactly one such i exists.

Example 1

g 3
returns 2

Position 2 gives 2 ^ 1 = 3, so the position of 3 is 2.

Example 2

g 2
returns 3

Position 3 gives 3 ^ 1 = 2, so the position of 2 is 3.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Bits problems