[Numpy-discussion] Loading bit strings

Robert Kern robert.kern@gmail....
Fri Mar 5 11:29:03 CST 2010


On Fri, Mar 5, 2010 at 11:11, Dan Lenski <dlenski@gmail.com> wrote:
> Is there a good way in NumPy to convert from a bit string to a boolean
> array?
>
> For example, if I have a 2-byte string s='\xfd\x32', I want to get a
> 16-length boolean array out of it.
>
> Here's what I came up with:
>
> A = fromstring(s, dtype=uint8)
> out = empty(A.size * 8, dtype=bool)
> for bit in range(0,8):
>  out[bit::8] = A&(1<<bit)
>
> I just can't shake the feeling that there may be a better way to
> do this, though...

For short enough strings, it probably doesn't really matter. Any
correct way will do.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the NumPy-Discussion mailing list