[Numpy-discussion] convert integer into bit array

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


On Tue, May 17, 2011 at 8:05 AM, Pearu Peterson <pearu.peterson at gmail.com>wrote:

>
>
> 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.
>

>>> numpy.array(barr)
    array([ True,  True, False, False], dtype=bool)

to complete the example...

Pearu


> 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/1358c1d8/attachment.html>


More information about the NumPy-Discussion mailing list