将 Double 转换为二进制表示?

我尝试将 double 转换为其二进制表示,但使用此 Long.toBinaryString(Double.doubleToRawLongBits(d)) 并没有帮助,因为我有大量数字,Long 可以t 存储它们,即 2^900.

I tried to convert a double to its binary representation, but using this Long.toBinaryString(Double.doubleToRawLongBits(d)) doesn't help, since I have large numbers, that Long can't store them i.e 2^900.

推荐答案

Long.toBinaryString(Double.doubleToRawLongBits(d)) 似乎工作得很好.

System.out.println("0:                0b" + Long.toBinaryString(Double.doubleToRawLongBits(0D)));
System.out.println("1:                0b" + Long.toBinaryString(Double.doubleToRawLongBits(1D)));
System.out.println("2:                0b" + Long.toBinaryString(Double.doubleToRawLongBits(2D)));
System.out.println("2^900:            0b" + Long.toBinaryString(Double.doubleToRawLongBits(Math.pow(2, 900))));
System.out.println("Double.MAX_VALUE: 0b" + Long.toBinaryString(Double.doubleToRawLongBits(Double.MAX_VALUE)));

/*
    prints:
    0:                0b0
    1:                0b11111111110000000000000000000000000000000000000000000000000000
    2:                0b100000000000000000000000000000000000000000000000000000000000000
    2^900:            0b111100000110000000000000000000000000000000000000000000000000000
    Double.MAX_VALUE: 0b111111111101111111111111111111111111111111111111111111111111111
*/

相关文章