[Numpy-discussion] convert integer into bit array

Pearu Peterson pearu.peterson at gmail.com
Tue May 17 01:05:17 EDT 2011


On Tue, May 17, 2011 at 12:04 AM, Nikolas Tautenhahn <virtual at gmx.de> wrote:

> Hi,
>
> > Here
> >
> >
> http://code.google.com/p/pylibtiff/source/browse/#svn%2Ftrunk%2Flibtiff%2Fbitarray-0.3.5-numpy
> >
> > you can find bitarray with numpy support.
> >
>
> Thanks, that looks promising - to get a numpy array, I need to do
>
> numpy.array(bitarray.bitarray(numpy.binary_repr(i, l)))
>
> for an integer i and l with i < 2**l, right?
>
>
If l < 64 and little endian is assumed then you can use the

  fromword(i, l)

method:

>>> from libtiff import bitarray
>>> barr = bitarray.bitarray(0, 'little')
>>> barr.fromword(3,4)
>>> barr
    bitarray('1100')

that will append 4 bits of the value 3 to the bitarray barr.

Also check out various bitarray `to*` and `from*` methods.

HTH,
Pearu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110517/8163c85b/attachment.html>


More information about the NumPy-Discussion mailing list