Integer to bitfield as a list

前端 未结 6 727
一向
一向 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:00

    Try

    >>>n=1794
    >>>bitfield=list(bin(n))[2:]
    >>>bitfield
    ['1', '1', '1', '0', '0', '0', '0', '0', '0', '1', '0']
    

    This does not work for negative n though and as you see gives you a list of strings

提交回复
热议问题