Created
May 29, 2017 15:09
-
-
Save fernandosavio/395cc1a8b4bbac2209d111b8e2a47a7a 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
def int_to_bin(numero, bit=1, string=''): | |
str_bit = '1' if bool(numero & bit) else '0' | |
string = str_bit + string | |
if bit > numero: | |
return string | |
else: | |
return int_to_bin(numero, bit << 1, string) | |
print(int_to_bin(1)) # 01 | |
print(int_to_bin(22)) # 010110 | |
print(int_to_bin(100)) # 01100100 | |
print(int_to_bin(1024)) # 010000000000 | |
print(int_to_bin(99999)) # 011000011010011111 | |
print(int_to_bin(123456)) # 011110001001000000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment