Skip to content

Instantly share code, notes, and snippets.

@jeanlaurent
Created November 5, 2023 17:36
Show Gist options
  • Save jeanlaurent/36c710eb13151820e3dd49f4759cabf7 to your computer and use it in GitHub Desktop.
Save jeanlaurent/36c710eb13151820e3dd49f4759cabf7 to your computer and use it in GitHub Desktop.
import bitstring
import math
def binary2Int(binary:str):
number = 0
for i in range(len(binary)):
if binary[i] == "1":
number += 2 ** (len(binary) - i - 1)
return number
def int2Binary(number:int):
binary = ""
while(number > 0):
if number % 2 == 1:
binary += "1"
else:
binary += "0"
number = number // 2
return binary[::-1]
def binary2Float(binary:str):
signe = binary[0]
if signe == "1":
signeValue = -1
else:
signeValue = 1
exposant = binary[1:9]
mantisse = binary[9:]
exposantValue = binary2Int(exposant) - 127
mantisseValue = 0
for i in range(len(mantisse)):
if mantisse[i] == "1":
mantisseValue += 2 ** -(i + 1)
return float(signeValue * (2 ** exposantValue) * (1 + mantisseValue))
def float2Binary(number:float):
signe = ""
mantisse = ""
# calcul du signe en binaire
if number > 0:
signe = "0"
else:
signe = "1"
decimale, entiere = math.modf(number)
# Calcule de l'exposant en binaire
valeurEntiereEnBinaire = int2Binary(int(entiere))
exposantEnBinaire = int2Binary(len(valeurEntiereEnBinaire)-1 + 127)
if len(exposantEnBinaire) < 8:
exposantEnBinaire = "0" * (8 - len(exposantEnBinaire)) + exposantEnBinaire
# Calcule de la mantisse en binaire
valeurDecimaleEnBinaire = ""
while(decimale > 0):
decimale = decimale * 2
if decimale >= 1:
valeurDecimaleEnBinaire += "1"
decimale -= 1
else:
valeurDecimaleEnBinaire += "0"
mantisse = valeurEntiereEnBinaire[1:] + valeurDecimaleEnBinaire
if len(mantisse) < 23:
mantisse += "0" * (23 - len(mantisse))
if len(mantisse) > 23:
mantisse = mantisse[:23]
return signe + exposantEnBinaire + mantisse
print("----------------- 1.0 -------------------")
print(bitstring.BitArray(float=1.0, length=32).bin)
print(bitstring.BitArray(bin="00111111100000000000000000000000").float)
print(binary2Float("00111111100000000000000000000000"))
print(float2Binary(1.0))
print("---------------- 1.1 --------------------")
print(bitstring.BitArray(float=1.1, length=32).bin)
print(bitstring.BitArray(bin="00111111100011001100110011001101").float)
print(binary2Float("00111111100011001100110011001101"))
print(float2Binary(1.1))
print("---------------- -0.5 ---------------------")
print(bitstring.BitArray(float=-0.5, length=32).bin)
print(bitstring.BitArray(bin="10111111000000000000000000000000").float)
print(binary2Float("10111111000000000000000000000000"))
print(float2Binary(-0.5))
print("-------------- 10.5 ----------------------")
print(bitstring.BitArray(float=10.5, length=32).bin)
print(bitstring.BitArray(bin="01000001001010000000000000000000").float)
print(binary2Float("01000001001010000000000000000000"))
print(float2Binary(10.5))
print("-------------- 10.6 ----------------------")
print(bitstring.BitArray(float=10.6, length=32).bin)
print(bitstring.BitArray(bin="01000001001010011001100110011010").float)
print(binary2Float("01000001001010011001100110011010"))
print(float2Binary(10.6))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment