在Java中Double.doubleToLongBits()
对于实现hashCode()
方法非常有用。
我正在尝试在C ++中执行相同的操作并编写自己的doubleToRawLongBits()
方法,因为在浏览Google之后我无法找到合适的实现。
我可以从std::frexp(numbr,&exp)
获取signif和exponent并且可以确定符号,但无法弄清楚使用按位运算符来获得Java等价物。
例如,Java的Double.doubleToLongBits()
返回以下双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 "canonical" 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);
答案 0 :(得分:8)
简单演员会做:
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]);
符号位和指数位取决于您的平台和字节顺序。如果您的浮点数是IEE754,如果符号和指数位于前面且CHAR_BIT == 8
,则可以尝试:
const bool sign = buf[0] & 0x80;
const int exponent = ((buf[0] & 0x7F) << 4) + (buf[1] >> 4) - 1023;
(在C中,为演员说(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)
的静态断言应该做的伎俩),如果整数实际上更大,那么你'只是复制到它的一部分。我相信你会明白这一点,但是:-)(你可以使用一些模板魔术来创建一个正确大小的整数,如果你真的想要的话。)
答案 1 :(得分:4)
#include <stdint.h>
static inline uint64_t doubleToRawBits(double x) {
uint64_t bits;
memcpy(&bits, &x, sizeof bits);
return bits;
}
答案 2 :(得分:2)
我喜欢这些事情的工会。
union double_and_buffer {
double d;
unsigned char byte_buff[ sizeof(double) ];
} dab;
dab.d = 1.0;
for ( int i = 0; i < sizeof(dab.byte_buff); ++i )
{
cout << hex byte_buff[ i ];
}
我认为它会让你更清楚自己在做什么,并让编译器完成所有数学运算。