I have integers in the range 0..2**m - 1
and I would like to convert them to binary numpy arrays of length m
. For example, say m = 4
. Now 15 = 1111
in binary and so the output should be (1,1,1,1)
. 2 = 10
in binary and so the output should be (0,0,1,0
). If m
were 3
then 2
should be converted to (0,1,0)
.
I tried np.unpackbits(np.uint8(num))
but that doesn't give an array of the right length. For example,
np.unpackbits(np.uint8(15))
Out[5]: array([0, 0, 0, 0, 1, 1, 1, 1], dtype=uint8)
I would like a method that worked for whatever m
I have in the code.