gpt4 book ai didi

java - 将 double 转换为二进制表示?

转载 作者:搜寻专家 更新时间:2023-10-30 21:16:28 24 4
gpt4 key购买 nike

我试图将 double 转换为其二进制表示,但使用此 Long.toBinaryString(Double.doubleToRawLongBits(d)) 没有帮助,因为我有大量数字,Long 不能不存储它们,即 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
*/

关于java - 将 double 转换为二进制表示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6359847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com