在Java语言中,Double.doubleToLongBits()
对于实现hashCode()
方法非常有用。
我正在尝试用C++做同样的事情,并编写我自己的doubleToRawLongBits()
方法,因为在搜索了谷歌之后,我找不到合适的实现。
我可以从std::frexp(numbr,&exp)
获得signif和指数,并且可以确定符号,但是不知道如何使用按位运算符来获得与之等价的符号。
例如,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 "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);
发布于 2011-10-31 23:51:29
#include <stdint.h>
static inline uint64_t doubleToRawBits(double x) {
uint64_t bits;
memcpy(&bits, &x, sizeof bits);
return bits;
}
发布于 2011-10-31 23:38:52
一个简单的造型就可以了:
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)
的静态断言应该可以做到这一点),如果整数实际上更大,那么您只需复制到其中的一部分。我相信你会弄明白的:-) (如果你真的想要的话,你可以使用一些模板魔术来创建一个大小正确的整数。)
发布于 2011-11-01 01:14:03
我喜欢工会来做这类事情。
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 ];
}
我认为这会让你更清楚的知道你在做什么,让编译器来做所有的数学运算。
https://stackoverflow.com/questions/7955933
复制相似问题