# [Numpy-discussion] int to binary

josef.pktd at gmail.com josef.pktd at gmail.com
Mon Apr 29 11:48:07 EDT 2013

```On Mon, Apr 29, 2013 at 11:25 AM, Sebastian Berg
<sebastian at sipsolutions.net> wrote:
> On Mon, 2013-04-29 at 11:15 -0400, josef.pktd at gmail.com wrote:
>>  Is there a available function to convert an int to binary
>> representation as sequence of 0 and 1?
>>
>
> Maybe unpackbits/packbits? It only supports the uint8 type, but you can
> view anything as that (being aware of endianess where necessary). You
> will also have to reshape the result, but that should not be a problem.

endianess sounds scary,
maybe too close to the memory layout for my taste (for me to maintain
as a helper function)

>>> k=3; np.unpackbits(np.arange(2**k, dtype=np.uint32).view(np.uint8), axis=-1).reshape(2**k,-1)[:, 4:8]
array([[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]], dtype=uint8)

>>> k=3; np.unpackbits(np.arange(256, 256+2**k, dtype=np.uint32).view(np.uint8), axis=-1).reshape(2**k,-1)[-1,:]
array([0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=uint8)
>>> k=3; np.unpackbits(np.arange(256, 256+2**k, dtype=np.uint32).view(np.uint8), axis=-1).reshape(2**k,-1).sum(0)
array([0, 0, 0, 0, 0, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=uint32)

Thanks,

Josef

>
> - Sebastian
>
>>
>>  binary_repr produces strings and is not vectorized
>>
>> >>> np.binary_repr(5)
>> '101'
>> >>> np.binary_repr(5, width=4)
>> '0101'
>> >>> np.binary_repr(np.arange(5), width=4)
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>>   File "C:\Python26\lib\site-packages\numpy\core\numeric.py", line
>> 1732, in binary_repr
>>     if num < 0:
>> ValueError: The truth value of an array with more than one element is
>> ambiguous. Use a.any() or a.all()
>>
>> ------------
>> That's the best I could come up with in a few minutes:
>>
>>
>> >>> k = 3;  int2bin(np.arange(2**k), k, roll=False)
>> array([[ 0.,  0.,  0.],
>>        [ 1.,  0.,  0.],
>>        [ 0.,  0.,  1.],
>>        [ 1.,  0.,  1.],
>>        [ 0.,  1.,  0.],
>>        [ 1.,  1.,  0.],
>>        [ 0.,  1.,  1.],
>>        [ 1.,  1.,  1.]])
>> >>> k = 3;  int2bin(np.arange(2**k), k, roll=True)
>> array([[ 0.,  0.,  0.],
>>        [ 0.,  0.,  1.],
>>        [ 0.,  1.,  0.],
>>        [ 0.,  1.,  1.],
>>        [ 1.,  0.,  0.],
>>        [ 1.,  0.,  1.],
>>        [ 1.,  1.,  0.],
>>        [ 1.,  1.,  1.]])
>>
>> -----------
>> def int2bin(x, width, roll=True):
>>     x = np.atleast_1d(x)
>>     res = np.zeros(x.shape + (width,) )
>>     for i in range(width):
>>         x, r = divmod(x, 2)
>>         res[..., -i] = r
>>     if roll:
>>         res = np.roll(res, width-1, axis=-1)
>>     return res
>>
>>
>> Josef
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion

```