How to convert an integer to a list of bits in Python

I would like to know if there is a better way to do this than my current method.

I am trying to represent an integer as a list of bits and leave it up to 8 bits only if the integer is <128

Example input: 0x15 Desired output: [0, 0, 0, 1, 0, 1, 0, 1] 

I do it as follows:

 input = 0x15 output = deque([int(i) for i in list(bin(input))[2:]]) while len(output) != 8: output.appendleft(0) 

Is there a better way to do this in python?

EDIT: I would like to convert any integer to a binary list. Pad up to 8 only if it requires less than 8 bits.

 Another Example input: 0x715 Desired output: [1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1] 
+5
source share
6 answers
 input = 0x15 output = [int(x) for x in '{:08b}'.format(input)] 

{0:0=8b}'.format(0x15) represents your input format in binary from 0 padding to 8 digits, and then using the list definition to create a list of bits.

Alternatively, you can use the map function:

output = map(int, [x for x in '{:08b}'.format(0x15)])

EDIT: variable width width,

If you want to make the number of bits of a variable, this is one way:

 width = 8 #8bit width output = [int(x) for x in '{:0{size}b}'.format(0x15,size=width)] output = map(int, [x for x in '{:0{size}b}'.format(0x15,size=width)]) 

This has been tested in Python 2.7.

+2
source

For a fixed size of 8 bits:

 num = 0x15 out = [1 if num & (1 << (7-n)) else 0 for n in range(8)] 

(1 << (7-n)) creates a one-bit mask for the given position, and then bit-by-bit & checks whether this bit is set in the number. Having n working from 0 to 7, outputs all 8 bits in a byte, which are checked in order.

For numbers of arbitrary size:

 import math num = 0x715 bits = int(max(8, math.log(num, 2)+1)) out = [1 if num & (1 << (bits-1-n)) else 0 for n in range(bits)] 
+5
source
 >>> [int(n) for n in bin(0x15)[2:].zfill(8)] [0, 0, 0, 1, 0, 1, 0, 1] 

Slice [2:] should remove the 0b prefix, zfill(8) is the zero petals.

+5
source

This is easy to do with format strings.

 >>> "{:08b}".format(0x15) '00010101' >>> "{:08b}".format(0x151) '101010001' >>> "{:08b}".format(0x1511) '1010100010001' 

to convert to list

 >>> [1 if x=='1' else 0 for x in "{:08b}".format(0x15)] [0, 0, 0, 1, 0, 1, 0, 1] >>> [1 if x=='1' else 0 for x in "{:08b}".format(0x1511)] [1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1] 

Most likely, it will be faster if you use the twiddling bit, as in @Amber's answer, but then you will have to check for special cases and get quite a lot of code in the end. If maximum performance is not required, it is safer to use what you already know.

+2
source

You can shift the number of steps x to the right, and then do bitwise, and the result is 1, to get a bit at position x , do it with a list and get your list. If you need to support negative numbers, we may need to add a null list to the list to ensure that positive numbers do not start with 1:

 import math def bits(n): # The number of bits we need to represent the number num_bits = max(8, int(math.log(abs(n), 2)) + 1) # The bit representation of the number bits = [ (n >> i) & 1 for i in range(num_bits) ] bits.reverse() # Do we need a leading zero? if n < 0 or bits[0] == 0: return bits return [0] + bits # Examples for n in (-0x15, 0x15, 128, 255, 256, -256): print("{: 4} = {}".format(n, bits(n))) 
  -21 = [1, 1, 1, 0, 1, 0, 1, 1] 21 = [0, 0, 0, 1, 0, 1, 0, 1] 128 = [0, 1, 0, 0, 0, 0, 0, 0, 0] 255 = [0, 1, 1, 1, 1, 1, 1, 1, 1] 256 = [0, 1, 0, 0, 0, 0, 0, 0, 0, 0] -256 = [1, 0, 0, 0, 0, 0, 0, 0, 0] 
0
source
 from math import ceil input = 0x15 bin_string = bin(input)[2:] binary = map(int,bin_string.zfill(int(ceil(len(bin_string)/8.0)*8))) print(binary) 

This will be rounded to the nearest multiple of 8, if you want to round to a multiple of 8, only if <128, use the simple if else statement and remove zfill in else

Output for 0x15:

 [0, 0, 0, 1, 0, 1, 0, 1] 

Output for 0x715:

 [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1] 

If you want to add zeros if less than 128, use this:

 input = 0x715 bin_string = bin(input)[2:] num_bits = (8 if input < 128 else 0) binary = map(int,bin_string.zfill(num_bits)) print(binary) 

Output for 0x15:

 [0, 0, 0, 1, 0, 1, 0, 1] 

Output for 0x715:

 [1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1] 
0
source

All Articles