Skip to content

Instantly share code, notes, and snippets.

@jameskyle
Created February 8, 2016 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameskyle/baece9e836cca869dd4b to your computer and use it in GitHub Desktop.
Save jameskyle/baece9e836cca869dd4b to your computer and use it in GitHub Desktop.
from __future__ import print_function
import sys
def convert(num):
bit_length = num.bit_length()
for x in reversed(range(bit_length)):
bit = (num >> x) & 1
print("{0}".format(bit), end="")
print("")
def recursive_convert(num, bit_length=None):
if bit_length is None:
bit_length = num.bit_length() - 1
bit = (num >> bit_length) & 1
print("{0}".format(bit), end="")
if bit_length != 0:
recursive_convert(num, bit_length - 1)
else:
print("")
if __name__ == "__main__":
num = int(sys.argv[1])
convert(num)
recursive_convert(num)
print("".join(["{0}".format((num >> x) & 1) for x in reversed(range(num.bit_length()))]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment