Bit Manipulation - Gray Code
Examine this Python function intended to generate an n-bit Gray code sequence:
What is the subtle bug causing incorrect Gray code output?
def gray_code(n):
if n == 0:
return [0]
prev = gray_code(n - 1)
add = 1 << n
return prev + [x + add for x in reversed(prev)]What is the subtle bug causing incorrect Gray code output?
