Created
January 13, 2020 07:18
-
-
Save anmolj7/70627d470227c95e497d8993ce4d2bfb to your computer and use it in GitHub Desktop.
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
#Well, The task description said that | |
#NOT SOOO FASSTTTT! | |
def shift_bits(num, n): | |
''' | |
a function written to shift the bits of the given number by n | |
for example, | |
1 -> 1 | |
1 << n = 1......0(n zeroes!) | |
''' | |
num = bin(num)[2:] #cause it returns strings like, 0b1 | |
num += '0'*n | |
return int(num, base=2) | |
def pow(n): | |
''' | |
Computes 2^n | |
The function could just use the bit shifting, 1 << n would give, 2 raised to power n. | |
Or, I could write a recursive power function, but I thought, writing a bit shifting fun is kinda more fun! | |
''' | |
return shift_bits(1, n) | |
def main(): | |
num = int(input('Enter the value of n: ')) | |
print(pow(num)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment