Express a number as a sum of powers of 2
List building performance aside, there is a way to get the powers of two out without touching the bits in between, by using the formulas:

isolateLowestSetBit(x) = x &amp; -x
resetLowestSetBit(x) = x &amp; (x - 1)


These have been explained elsewhere.

Using those definitions, you can extract the powers of two like this:

def two_powers(num):
    powers = []
    while num != 0:
        powers.append(num &amp; -num)
        num = num &amp; (num - 1)
    return powers


Since it potentially avoids testing many bits (depending on the size and sparseness of the input) it may be faster. It can also work out not so well, for very dense numbers.