Integer to bitfield as a list

前端 未结 6 719
一向
一向 2021-02-13 01:33

I\'ve created a method to convert an int to a bitfield (in a list) and it works, but I\'m sure there is more elegant solution- I\'ve just been staring at it for to

6条回答
  •  不知归路
    2021-02-13 02:22

    Array with fixed length

    Array with fixed length:

    >>> '{0:07b}'.format(12)
    '0001100'
    

    Do you believe string is also an array? No? See:

    >>> [int(x) for x in '{0:07b}'.format(12)]
    [0, 0, 0, 1, 1, 0, 0]
    

提交回复
热议问题