Skip to content

Instantly share code, notes, and snippets.

@viveksyngh
Created August 15, 2015 16:13
Show Gist options
  • Select an option

  • Save viveksyngh/f8e93c269602b940337f to your computer and use it in GitHub Desktop.

Select an option

Save viveksyngh/f8e93c269602b940337f to your computer and use it in GitHub Desktop.
Find if Given number is power of 2 or not.
__author__ = 'Vivek'
#Find if Given number is power of 2 or not.
#More specifically, find if given number can be expressed as 2^k where k >= 1.
def power(A):
temp = int(A)
A = int(A)
Sum = 0
while A != 0 :
Sum += A%2
A = A/2
if Sum == 1 and temp != 1 :
return 1
else :
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment