Skip to content

Instantly share code, notes, and snippets.

@rrbutani
Last active March 2, 2021 22:15
Show Gist options
  • Save rrbutani/6777b03add4974810eea8e63ae80d11a to your computer and use it in GitHub Desktop.
Save rrbutani/6777b03add4974810eea8e63ae80d11a to your computer and use it in GitHub Desktop.
# $1: float, $2: override
function f2b() {
${2-python3} - << EOF
import struct, sys
from termcolor import colored
c = lambda s, col: colored(s, col) if sys.stdout.isatty() else s
def binary(num):
b = bin(struct.unpack('!I',struct.pack('!f', num))[0])
return b.replace('0b', '').rjust(32, '0')
o = binary($1)
s = o[0]
e = o[1:9]
m1 = o[9:16]
m2 = o[16:32]
b = f"0b{o}"
h = hex(int(b, 2))
print(f"0b{c(s, 'red')}{c(e, 'green')}{c(m1, 'blue')}{c(m2, 'magenta')} ({h})")
EOF
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment