Let\'s a=109 or 1101101 in binary. How do I iterate over bits of this number, eg: [64, 32, 8, 4, 1]
a=109
1101101
[64, 32, 8, 4, 1]
Python 2.7:
def binary_decomposition(x): p = 2 ** (int(x).bit_length() - 1) while p: if p & x: yield p p //= 2
Example:
>>> list(binary_decomposition(109)) [64, 32, 8, 4, 1]