Skip to content

Instantly share code, notes, and snippets.

@airglow923
Last active October 13, 2021 03:26
Show Gist options
  • Save airglow923/0264a81202fb615891b6550dd98b2fc9 to your computer and use it in GitHub Desktop.
Save airglow923/0264a81202fb615891b6550dd98b2fc9 to your computer and use it in GitHub Desktop.
Binary-related code written in Python
# Return a binary representation of an integer with space every 4 bits
def get_binary(x):
binary = bin(x)[2:]
for i in range(len(binary) - 4, 0, -4):
binary = binary[:i] + ' ' + binary[i:]
return binary
# Convert binary floating-point number into decimal
def binary_float_point_to_decimal(x):
result = 0.0
binary = bin(x)[2:]
for n, bit in enumerate(binary, start=1):
if bit == "1":
result = result + 1 / 2 ** n
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment