Skip to content

Instantly share code, notes, and snippets.

@hbradio
Created November 23, 2016 18:11
Show Gist options
  • Save hbradio/232ea5b1d3a3d0db731d44a7e948f807 to your computer and use it in GitHub Desktop.
Save hbradio/232ea5b1d3a3d0db731d44a7e948f807 to your computer and use it in GitHub Desktop.
def twos_compl(val):
lsb = 1.0/2**15
if (val < -1.0) or (val > 1.0-lsb):
raise ValueError("Out of range")
if (val > 0):
return np.binary_repr(int(val/lsb),16)
else:
return np.binary_repr(int(2**15 - (1.0 + val)/lsb), 16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment