Skip to content

Instantly share code, notes, and snippets.

@SealtielFreak
Created December 18, 2022 23:37
Show Gist options
  • Save SealtielFreak/c2da0ae6a37cdb91b94e2db369e82675 to your computer and use it in GitHub Desktop.
Save SealtielFreak/c2da0ae6a37cdb91b94e2db369e82675 to your computer and use it in GitHub Desktop.
def count_bits(number):
bit = 1
i = 0
while True:
if bit > n:
return i
bit += bit
i += 1
return -1
def int_to_bin(number):
bits = [*map(lambda i: (number >> i) % 2, reversed(range(count_bits(number))))]
return '0b' + ''.join([*map(str, bits)])
if __name__ == "__main__":
n = 512
print('int: %i\nbin: %s' % (n, int_to_bin(n)))
print('total bits: %i' % count_bits(n))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment