Last active
December 20, 2015 18:09
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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