Created
July 8, 2017 08:52
-
-
Save MrNocTV/19dd49c677affe93055a1252cc8da760 to your computer and use it in GitHub Desktop.
Next Power of 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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