Skip to content

Instantly share code, notes, and snippets.

@fernandosavio
Created May 29, 2017 15:09
Show Gist options
  • Save fernandosavio/395cc1a8b4bbac2209d111b8e2a47a7a to your computer and use it in GitHub Desktop.
Save fernandosavio/395cc1a8b4bbac2209d111b8e2a47a7a to your computer and use it in GitHub Desktop.
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