Skip to content

Instantly share code, notes, and snippets.

@Yepoleb
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yepoleb/5538f16dd35e13ede0fa to your computer and use it in GitHub Desktop.
Save Yepoleb/5538f16dd35e13ede0fa to your computer and use it in GitHub Desktop.
bincont = b"\x34\x83\x12\x6F"
def bitlist(inbytes):
output = []
for byte in reversed(inbytes):
for i in range(0, 8):
bit = byte & (1 << i)
if bit:
output.append(1)
else:
output.append(0)
return output
def addbits(bits):
out = 0
for bit in reversed(bits):
out = (out << 1) | bit
return out
def fractional_add(bits):
accum = 0
for i, bit in enumerate(reversed(bits)):
accum += bit*(1/(2**(i+1)))
return accum
def decfloat(floatbytes):
bits = bitlist(floatbytes)
sign = bits[31]
expb = bits[23:31]
fractb = bits[0:23]
exp = addbits(expb)
fract = fractional_add(fractb)
print(fractb, fract)
return (-1)**sign * 2**(exp-127) * (1 + fract)
print(decfloat(bincont))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment