Last active
October 13, 2021 03:26
-
-
Save airglow923/0264a81202fb615891b6550dd98b2fc9 to your computer and use it in GitHub Desktop.
Binary-related code written in Python
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
# 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