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

标签 java c++ c

在 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/

相关文章:

C++ 未定义对模板类方法的引用

c - C中使用calloc的段错误

通过树莓派切换套接字状态的Java函数

java抽象类的继承

Java值加变量++

c++ - 有没有办法找到像sizeof facelity这样的动态内存大小?

c - 数组函数只返回第一个元素,我需要整个数组。请帮助

java - 如何从文件读入并使用读取文件的输入创建新对象

c++ - 除非首先使用 MS Paint 打开/保存文件,否则带有 QRCode 位图的 LoadImage() 失败

c++ - 为什么这个子类代码会导致运行时错误?