Created
April 7, 2011 20:50
-
-
Save seadowg/908683 to your computer and use it in GitHub Desktop.
I once asked the fastest to calculate if a number was a power of two or not and I choked. Here is a nifty little way to do it.
This file contains hidden or 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
import sys | |
def isapoweroftwo(n): | |
if n == 0: | |
return False | |
if n & (n - 1) == 0: | |
return True | |
return False | |
if __name__ == "__main__": | |
print isapoweroftwo(int(sys.argv[1])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment