Created
December 18, 2022 23:37
-
-
Save SealtielFreak/c2da0ae6a37cdb91b94e2db369e82675 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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