Skip to content

Instantly share code, notes, and snippets.

@seadowg
Created April 7, 2011 20:50
Show Gist options
  • Save seadowg/908683 to your computer and use it in GitHub Desktop.
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.
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