Generating a Gray code representation of a binary number can be thought of as mapping one binary number onto another binary number. Therefore, $n$-bit Gray code is a permutation of $2^n$ elements.
What would be an efficient way to generate the corresponding permutation matrix (having sparsity 1)?
A brute-force solution would be to go through all the integers from $0$ to $2^{n-1}$, converting each of them from binary to Gray using smth like
def convert_gray(binary):
binary = int(binary, 2)
binary ^= (binary >> 1)
return bin(binary)[2:]
considering each outcome as a binary, and filling the corresponding matrix entry.
I am wondering if there exists a faster and a more compact solution?