Convert Binary Code to Python Digit List

I have the following script:

x = 0b0111 

I would like to convert this value to:

 y = [0, 1, 1, 1] 

When I convert x = 0b1001 , I can get y = [1, 0, 0, 1] , but when I try to do the same for x = 0b0111 , and then convert back with str(bin(y)) - I, seems to lose the leading 0 and get 0b111 .

Any suggestions?

+8
source share
9 answers

Once you get this line 0b111 , it’s easy for you to separate the numbers that interest you. For each character after only 0b in a string, convert it to an integer.

 [int(d) for d in str(bin(x))[2:]] 
+8
source

Whatever the cost, a purely arithmetic solution is a little faster:

 import timeit def bits1(n): b = [] while n: b = [n & 1] + b n >>= 1 return b or [0] timeit.timeit(lambda: bits1(12345678)) [out] 7.717339038848877 def bits2(n): return [int(x) for x in bin(n)[2:]] timeit.timeit(lambda: bits2(12345678)) [out] 10.297518014907837 

Update 2019: in python 3.7.3, the second version is slightly faster.

+5
source

First, convert the number to binary, and then to the string:

 str(bin(7)) '0b111' #note the 0b in front of the binary number 

Then remove 0b from the line

 str(bin(7))[2:] '111' 

Finally, we use list comprehension to create an int list from a string, which takes roughly the following form:

 [expr for i in iterable] [int(i) for i in str(bin(x))[2:]] 
+3
source

An expression like map(int, list(bin((1<<8)+x))[-4:]) will give you 4 bits of a number in the form of a list. (Edit: a cleaner map(int,bin(x)[2:].zfill(4)) form map(int,bin(x)[2:].zfill(4)) , see below.) If you know how many bits you want to show, replace 4 (in [-4:] ) on this number; and if necessary 8 (in (1<<8) ) increase the number. For instance:

 >>> x=0b0111 >>> map(int,list(bin((1<<8)+x))[-4:]) [0, 1, 1, 1] >>> x=37; map(int,list(bin((1<<8)+x))[-7:]) [0, 1, 0, 0, 1, 0, 1] >>> [int(d) for d in bin((1<<8)+x)[-7:]] [0, 1, 0, 0, 1, 0, 1] 

The final example above shows an alternative to using a map and list. The following examples show a slightly cleaner form for leading zeros. In these forms, replace the required minimum number of bits instead of 8.

 >>> x=37; [int(d) for d in bin(x)[2:].zfill(8)] [0, 0, 1, 0, 0, 1, 0, 1] >>> x=37; map(int,bin(x)[2:].zfill(8)) [0, 0, 1, 0, 0, 1, 0, 1] >>> x=37; map(int,bin(x)[2:].zfill(5)) [1, 0, 0, 1, 0, 1] >>> x=37; map(lambda k:(x>>-k)&1, range(-7,1)) [0, 0, 1, 0, 0, 1, 0, 1] 
+2
source

check out this easy way to do it ... for this particular scenario

 In [41]: x=0b0111 In [42]: l = [0,0,0,0] In [43]: counter = -1 In [44]: for i in str(bin(x))[2:]: ....: l[counter] = i ....: counter = counter -1 ....: In [45]: l Out[45]: [0, '1', '1', '1'] 
0
source
 c=[] for i in bin(7)[2:]: c.append(int(i)) #turning string "111", to 111 if len(c)==3: c.insert(0,0) print(c) # binary digit 7 produces '0b111' by this slicing[2:], result get '111' 

therefore, if the item in list c is 3, 0 is inserted first.

0
source

If the bit length is set to 4 , there are two solutions:

 [int(i) for i in '{0:04b}'.format(0b0111)] 

or with numpy,

 import numpy as np [int(i) for i in np.binary_repr(0b0111, 4)] 
0
source

Not very pythonic, but:

 byte = 0b00011001 arr = [] for i in range(7, -1, -1): arr.append((byte & 1<<i)>>i) print(arr) [0, 0, 0, 1, 1, 0, 0, 1] 

it uses a bitmask to “extract” each bit and add to the array.

0
source

Given a numeric value and the number of bits to represent it by width :

 string = format(value, '0{}b'.format(width)) binary_list = [0 if c == '0' else 1 for c in string] 

This is about a third faster than binary_list = [int(c) for c in string]

0
source

All Articles