views:

43

answers:

2

In JavaScript, for a given property containing a Number value, is it possible to retrieve the actual binary value - the 64 bits representing the value.

var x = 13;

What 64 bits are stored in the memory location that x points to?

I know that there are IEEE 754 converters out there. But is it possible to retrieve the actual live binary value from the memory cell? BTW, I don't need this for any application, I'm just curious...

A: 
(13).toString(2); // => "1101"
Gareth
This is the binary value of the decimal number 13. What I am talking about is the 64-bit IEEE 754 format. In the case of the decimal value 13, it should be this (according to a online converter): 01000000 00101010 00000000 00000000 00000000 00000000 00000000 00000000. http://www.binaryconvert.com/convert_double.html
Šime Vidas
+1  A: 

See http://stackoverflow.com/questions/3077718/converting-a-decimal-value-to-a-32bit-floating-point-hexadecimal/3117567#3117567, where you can find the code for the 32-bit case. Converting that for the 64-bit case should be really straight-forward.

Well, except for the fact that JavaScript doesn't guarantee you anything about the actual data type that represents a Number, so you might get overflows if your JavaScript implementation only uses 32 bits for representing Numbers.

Roland Illig