Question
How do I convert a decimal number into a binary list? For example, how do I change 8 into [1,0,0,0]
How-To
Try this:
Edit -- Ooops, you wanted integers:
Or you can do it this way:
- def i2b(x):
- if x == 0: return [0]
- bit = []
- while x:
- bit.append(x % 2)
- x >>= 1
- return bit[::-1]
Supplement
* Convert an integer to binary without using the built-in bin function
沒有留言:
張貼留言