Bit data can be represented through the Arrays pack , as Float does not provide functions internally.
str = [12.125].pack('D').bytes.reverse.map{|n| "%08b" %n }.join => "0100000000101000010000000000000000000000000000000000000000000000" [ str[0], str[1..11], str[12..63] ] => ["0", "10000000010", "1000010000000000000000000000000000000000000000000000"]
This is a little “around the house” to get it out of the string representation. I am sure there is a more efficient way to pull data from the original bytes ...
Change Bit level manipulation changed my interest, so I had a dream. To use operations in Ruby, you need to have Integer, so float requires a little more unpack ing to convert to a 64-bit int. The big end documentary / ieee 754 is pretty trivial. A little idea of the end I'm not so sure. This is a bit strange since you are not at full byte boundaries with an 11-bit metric and a 52-bit mantissa. It becomes hesitant to pull out the bits and swap them to get what looks like a little endian, and not sure if this is correct, since I have not seen any reference to the layout. Thus, the 64-bit value is little endian, I'm not too sure how this applies to the components of the 64-bit value until you save them in another place, for example, 16 bits of int for the mantissa.
As an example for an 11-bit value from little> big, the kind of thing I did was to move the most significant byte from left 3 to the front, then to OR with the least significant 3 bits.
v = 0x4F2 ((v & 0xFF) << 3) | ( v >> 8 ))
Anyway, I hope its use.
class Float Float::LITTLE_ENDIAN = [1.0].pack("E") == [1.0].pack("D")
and testing ...
def printer val, vals printf "%-15s sign|%01b|\n", val, vals[0] printf " hex e|%3x| m|%013x|\n", vals[1], vals[2] printf " bin e|%011b| m|%052b|\n\n", vals[1], vals[2] end floats = [ 12.125, -12.125, 1.0/3, -1.0/3, 1.0, -1.0, 1.131313131313, -1.131313131313 ] floats.each do |v| printer v, v.ieee745_binary64 printer v, v.ieee745_binary64_big end
TIL my brain is big andian! You will notice that the ints that it works with have a large endian. I failed by moving a little the other way.