Skip to content

Instantly share code, notes, and snippets.

@rennex
Last active December 20, 2015 18:09
Show Gist options
  • Save rennex/6173865 to your computer and use it in GitHub Desktop.
Save rennex/6173865 to your computer and use it in GitHub Desktop.
Get the bit representation of a double in Ruby. (pack it as "G" = big-endian double-precision float, unpack as "Q>" = big-endian 64-bit int)
def bits(x) "%064b" % ([x].pack("G").unpack("Q>")[0]) end
bits(0.1)
#=> "0011111110111001100110011001100110011001100110011001100110011010"
bits(-0.1)
#=> "1011111110111001100110011001100110011001100110011001100110011010"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment