Thursday, 15 August 2013

python - Return list of active bits -



python - Return list of active bits -

how can list of active bits (active bit when equal 1)?

example: number 220 in decimal 11011100 in binary should homecoming ['2', '3', '4', '6', '7'] because bits 0, 1 , 5 off .

alternative dirty 1 liner (no strings attached):

input_number = 220 print [i in xrange(input_number.bit_length()) if ((1 << i) & input_number)]

python bits

No comments:

Post a Comment