InferenceAI

Enter at least two characters to search.

Start free

Reflected Gray Code

mediumBit Manipulation25 min25 XP

Return the standard nn-bit reflected Gray code: 2n2^n integers starting at 0 where consecutive values (and the last and first) differ in exactly one bit. Use the formula g(i)=i⊕(i≫1)g(i) = i \oplus (i \gg 1).

Examples

EXAMPLE WALKTHROUGHfour codes

A guided view of the published example, not a trace of your code.

INPUT[2]
TASKreflected_gray_code()
EXPECTED OUTPUT[0,1,3,2]

Start with the input values shown in this example.

Example 1 of 3

Input: n = 2

Output: [0,1,3,2]

Explanation: four codes

Input: n = 1

Output: [0,1]

Explanation: one bit

Input: n = 0

Output: [0]

Explanation: zero bits

Hint 1

XOR each index with itself shifted right by one.

Topics

DSABit ManipulationPython