comnum.gray_code#

comnum.gray_code(n_bits)#

The reflected binary Gray code of n_bits bits: g[i] = i ^ (i >> 1).

Parameters:

n_bits (int) – the number of bits.

Returns:

the 2^n_bits code words (int64).

Return type:

numpy.ndarray

Raises:

ValueError – for more than 24 bits (more than 16 777 216 code words).

Examples

>>> import comnum
>>> comnum.gray_code(2).tolist()
[0, 1, 3, 2]