How to extract the bits of larger numeric Numpy data types

numpy, python

Solution

You can do this with `view` and `unpackbits`

Input:

unpackbits(arange(2, dtype=uint16).view(uint8))

Output:

[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0]

For `a = arange(int(1e6), dtype=uint16)` this is pretty fast at around 7 ms on my machine

%%timeit
unpackbits(a.view(uint8))

100 loops, best of 3: 7.03 ms per loop

As for endianness, you'll have to look at http://docs.scipy.org/doc/numpy/user/basics.byteswapping.html and apply the suggestions there depending on your needs.

Problem

Numpy has a library function, `np.unpackbits`, which will unpack a `uint8` into a bit vector of length 8. Is there a correspondingly fast way to unpack larger numeric types? E.g. `uint16` or `uint32`. I am working on a question that involves frequent translation between numbers, for array indexing, and their bit vector representations, and the bottleneck is our pack and unpack functions.

Original source

Related problems