Skip to content

Instantly share code, notes, and snippets.

@SteadBytes
Last active December 2, 2017 07:27
Show Gist options
  • Save SteadBytes/265d5d752679d76fadad3a49f1791db4 to your computer and use it in GitHub Desktop.
Save SteadBytes/265d5d752679d76fadad3a49f1791db4 to your computer and use it in GitHub Desktop.
def split_power_2(x):
""" Splits a number up into powers of 2, returning a list of powers.
"""
split_powers = []
i = 1
while i <= x:
if i & x: # 1 or 0 -> evaluate as True/False
split_powers.append(i)
i <<= 1
return split_powers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment