Skip to content

Instantly share code, notes, and snippets.

@phoenixperry
Last active January 3, 2016 20:35
Show Gist options
  • Save phoenixperry/54c3e0c3147fd643582c to your computer and use it in GitHub Desktop.
Save phoenixperry/54c3e0c3147fd643582c to your computer and use it in GitHub Desktop.
def powersOfTwo(x):
count = 1 #start at the first power of 2, 1
isAPowerOfTwo = False #have a nice little bool to see if we have a power of two for user output
if(x == 1): #a little test to see if we're dealing with 1, which is a bit of a special case
isAPowerOfTwo = True #filp the boolean so we know we have a power of two
for i in range(9): # we need to go through this 8 times b/c 512 is the 8th bit shift 000000000
#0000000000 = 0 (first time)
#0000000001 = 1 (time 1 through the loop)
#0000000010 = 2 (time 2 through the loop)
#0000000100 = 4 (time 3 through the loop)
#0000001000 = 8 (time 4 through the loop)
#0000010000 = 16 (time 5 through the loop)
#0000100000 = 32 (time 6 through the loop)
#0001000000 = 64 (time 7 through the loop)
#0010000000 = 128 (time 8 through the loop)
#0100000000 = 256 (time 9 through the loop)
#1000000000 = 512 (time 10 trhough, which is wehre we stop. Remember computers start counting at 0)
count = count << 1 #shift the bit over 1
print(str(count)) #print out the number
#if we have a number other than 1, we'll need to increase it if it's a power of two or tell the user they didn't input a power of two or they are using a power of two over our limit of 512
for i in range(1, 10): #start with 2, since we've checked for one already
count = count << 1 #bitshift 1 to the number 2
#0000000010 = 2 (time 1 through the loop)
#0000000100 = 4 (time 2 through the loop)
#0000001000 = 8 (time 3 through the loop)
#0000010000 = 16 (time 4 through the loop)
#0000100000 = 32 (time 5 through the loop)
#0001000000 = 64 (time 6 through the loop)
#0010000000 = 128 (time 7 through the loop)
#0100000000 = 256 (time 8 through the loop)
#1000000000 = 512 (time 9 trhough, which is wehre we stop. Remember computers start counting at 0)
if(x == count): #we have a power of two! we found it in the iteration loop.
print(str(x) + " is a power of two") # tell the user we found it
isAPowerOfTwo = True #change our little boolean switch so we can give the user feedback later if no power of two was found
if(x == 512): #check to see if we are already at our 512 limit, if so break out of the loop
print("This is our limit, no additional counting required")
break
else: #else, let's step through this current power of two and raise it all the way up to 512
for j in range(i+1, 10): #using a second for loop let's this happen. We just start with the number in front of where our power of two is to get the next number in the range. So if we have 4, 8 should be the next number so we want to move i forward 1 position in the array above and then go through that whole little bit shiftiing process until we hit 512.
count = count << 1
print(str(count))
if(isAPowerOfTwo==False): #ok we got all the way up to 512 in our main loop and we never hit 512. sorry!
print("you did not input a power of two or you used a power of 2 over 512")
#######################
#this is the main loop for the application
ran = False
while(ran == False):
try:
num = raw_input ("Enter a power of Two up to 512: ")
num = int(num)
powersOfTwo(num)
ran = True
except ValueError:
print "Oops! That was no valid number. Try again..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment