gpt4 book ai didi

java - C/C++ 相当于 Java 的 doubleToRawLongBits()

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:58:25 25 4
gpt4 key购买 nike

在 Java 中,Double.doubleToLongBits() 可用于实现 hashCode() 方法。

我试图在 C++ 中做同样的事情并编写我自己的 doubleToRawLongBits() 方法,因为在通过 Google 搜索后我找不到合适的实现。

我可以从 std::frexp(numbr,&exp) 获取符号和指数,并且可以确定符号,但无法弄清楚如何使用按位运算符来获取 Java 等效项。

例如,Java 的 Double.doubleToLongBits() 为 double 3.94 返回以下内容:

4616054510065937285

感谢您的帮助。

格雷厄姆

下面是从 Double.doubleToRawLongBits() 复制粘贴的文档

===Java Double.doubleToRawLongBits() description===

/**
* Returns a representation of the specified floating-point value
* according to the IEEE 754 floating-point "double
* format" bit layout, preserving Not-a-Number (NaN) values.
* <p>
* Bit 63 (the bit that is selected by the mask
* <code>0x8000000000000000L</code>) represents the sign of the
* floating-point number. Bits
* 62-52 (the bits that are selected by the mask
* <code>0x7ff0000000000000L</code>) represent the exponent. Bits 51-0
* (the bits that are selected by the mask
* <code>0x000fffffffffffffL</code>) represent the significand
* (sometimes called the mantissa) of the floating-point number.
* <p>
* If the argument is positive infinity, the result is
* <code>0x7ff0000000000000L</code>.
* <p>
* If the argument is negative infinity, the result is
* <code>0xfff0000000000000L</code>.
* <p>
* If the argument is NaN, the result is the <code>long</code>
* integer representing the actual NaN value. Unlike the
* <code>doubleToLongBits</code> method,
* <code>doubleToRawLongBits</code> does not collapse all the bit
* patterns encoding a NaN to a single &quot;canonical&quot; NaN
* value.
* <p>
* In all cases, the result is a <code>long</code> integer that,
* when given to the {@link #longBitsToDouble(long)} method, will
* produce a floating-point value the same as the argument to
* <code>doubleToRawLongBits</code>.
*
* @param value a <code>double</code> precision floating-point number.
* @return the bits that represent the floating-point number.
* @since 1.3
*/
public static native long doubleToRawLongBits(double value);

最佳答案

一个简单的转换就可以了:

double d = 0.5;

const unsigned char * buf = reinterpret_cast<const unsigned char *>(&d);

for (unsigned int i = 0; i != sizeof(double); ++i)
std::printf("The byte at position %u is 0x%02X.\n", i, buf[i]);

符号位和指数位的位置取决于您的平台和字节序。如果你的 float 是 IEE754,如果符号和指数在前面,如果 CHAR_BIT == 8 ,你可以试试这个:

const bool sign = buf[0] & 0x80;
const int exponent = ((buf[0] & 0x7F) << 4) + (buf[1] >> 4) - 1023;

(在 C 中,对 Actor 说 (const unsigned char *)(&d)。)

更新:要创建具有相同位的整数,您必须先生成整数,然后复制:

unsigned long long int u;
unsigned char * pu = reinterpret_cast<unsigned char *>(&u);
std::copy(buf, buf + sizeof(double), pu);

为此,您必须牢记几件事:整数的大小必须足够(sizeof(double) <= sizeof(unsigned long long int) 的静态断言应该可以解决问题),如果整数实际上更大,那么您只复制到它的一部分。不过,我相信您会弄明白的:-)(如果您确实需要,可以使用一些模板魔术来创建正确大小的整数。)

关于java - C/C++ 相当于 Java 的 doubleToRawLongBits(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7955933/

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