# [Numpy-discussion] convert integer into bit array

Tom Krauss thomas.p.krauss at gmail.com
Mon May 16 19:06:25 EDT 2011

```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 <virtual at gmx.de> wrote:
> Hi,
>
>> Here
>>
>>
>> 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 at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

```