Skip to content

Instantly share code, notes, and snippets.

@MrNocTV
Created July 8, 2017 08:52
Show Gist options
  • Save MrNocTV/19dd49c677affe93055a1252cc8da760 to your computer and use it in GitHub Desktop.
Save MrNocTV/19dd49c677affe93055a1252cc8da760 to your computer and use it in GitHub Desktop.
Next Power of 2
def is_power_of_2(n):
return n > 0 and (n & (n-1)) == 0
def next_power_of_2(n):
if is_power_of_2(n):
return n
count = 0
while n > 0:
count += 1
n = n >> 1
return 1 << count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment