bit_manipulation.gray_code_sequence¶
Functions¶
|
Takes in an integer n and returns a n-bit |
|
Will output the n-bit Gray code sequence as a |
Module Contents¶
- bit_manipulation.gray_code_sequence.gray_code(bit_count: int) list[int]¶
Takes in an integer n and returns a n-bit gray code sequence An n-bit gray code sequence is a sequence of 2^n integers where:
Every integer is between [0,2^n -1] inclusive
The sequence begins with 0
An integer appears at most one time in the sequence
The binary representation of every pair of integers differ by exactly one bit
The binary representation of first and last bit also differ by exactly one bit
>>> gray_code(0) [0]
>>> gray_code(2) [0, 1, 3, 2]
>>> gray_code(1) [0, 1]
>>> gray_code(3) [0, 1, 3, 2, 6, 7, 5, 4]
>>> gray_code(-1) Traceback (most recent call last): ... ValueError: The given input must be positive
>>> gray_code(10.6) Traceback (most recent call last): ... TypeError: unsupported operand type(s) for <<: 'int' and 'float'