convert integer into bit array
Hi, for some research, I need to convert lots of integers into their bit representation - but as a bit array, not a string like numpy.binary_repr() returns it. So instead of In [22]: numpy.binary_repr(23) Out[22]: '10111 I'd need: numpy.binary_magic(23) Out: array([ True, False, True, True, True], dtype=bool) is there any way to do this efficiently? best regards, Nik
Hi,
I have used bitarray for that
http://pypi.python.org/pypi/bitarray/
Here
http://code.google.com/p/pylibtiff/source/browse/#svn%2Ftrunk%2Flibtiff%2Fbi...
you can find bitarray with numpy support.
HTH,
Pearu
On Mon, May 16, 2011 at 9:55 PM, Nikolas Tautenhahn
Hi,
for some research, I need to convert lots of integers into their bit representation - but as a bit array, not a string like numpy.binary_repr() returns it.
So instead of In [22]: numpy.binary_repr(23) Out[22]: '10111
I'd need: numpy.binary_magic(23) Out: array([ True, False, True, True, True], dtype=bool)
is there any way to do this efficiently?
best regards, Nik _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Hi,
Here
http://code.google.com/p/pylibtiff/source/browse/#svn%2Ftrunk%2Flibtiff%2Fbi...
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? best regards, Nik
Here's a one-liner:
In [31]: x=np.arange(-8,8)
In [32]: x
Out[32]: array([-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7])
In [33]: 1&(x[:,np.newaxis]/2**np.arange(3,-1,-1))
Out[33]:
array([[1, 0, 0, 0],
[1, 0, 0, 1],
[1, 0, 1, 0],
[1, 0, 1, 1],
[1, 1, 0, 0],
[1, 1, 0, 1],
[1, 1, 1, 0],
[1, 1, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 1, 0, 0],
[0, 1, 0, 1],
[0, 1, 1, 0],
[0, 1, 1, 1]])
On Mon, May 16, 2011 at 4:04 PM, Nikolas Tautenhahn
Hi,
Here
http://code.google.com/p/pylibtiff/source/browse/#svn%2Ftrunk%2Flibtiff%2Fbi...
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?
best regards, Nik _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On Tue, May 17, 2011 at 12:04 AM, Nikolas Tautenhahn
Hi,
Here
http://code.google.com/p/pylibtiff/source/browse/#svn%2Ftrunk%2Flibtiff%2Fbi...
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
On Tue, May 17, 2011 at 8:05 AM, Pearu Peterson
On Tue, May 17, 2011 at 12:04 AM, Nikolas Tautenhahn
wrote: Hi,
Here
http://code.google.com/p/pylibtiff/source/browse/#svn%2Ftrunk%2Flibtiff%2Fbi...
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
participants (3)
-
Nikolas Tautenhahn
-
Pearu Peterson
-
Tom Krauss