简体   繁体   English

使用std :: bitset进行双重表示

[英]Using std::bitset for double representation

In my application i'm trying to display the bit representation of double variables. 在我的应用程序中,我试图显示双精度变量的位表示。 It works for smaller double variables. 它适用于较小的双变量。 Not working for 10^30 level. 不适用于10 ^ 30级别。

Code: 码:

#include <iostream>
#include <bitset>
#include <limits>
#include <string.h>

using namespace std;

void Display(double doubleValue)
{
    bitset<sizeof(double) * 8> b(doubleValue);
    cout << "Value  : " << doubleValue << endl;
    cout << "BitSet : " << b.to_string() << endl;
}

int main()
{
    Display(1000000000.0);
    Display(2000000000.0);
    Display(3000000000.0);

    Display(1000000000000000000000000000000.0);
    Display(2000000000000000000000000000000.0);
    Display(3000000000000000000000000000000.0);

    return 0;   
}

Output: 输出:

/home/sujith% ./a.out
Value  : 1e+09
BitSet : 0000000000000000000000000000000000111011100110101100101000000000
Value  : 2e+09
BitSet : 0000000000000000000000000000000001110111001101011001010000000000
Value  : 3e+09
BitSet : 0000000000000000000000000000000010110010110100000101111000000000
Value  : 1e+30
BitSet : 0000000000000000000000000000000000000000000000000000000000000000
Value  : 2e+30
BitSet : 0000000000000000000000000000000000000000000000000000000000000000
Value  : 3e+30
BitSet : 0000000000000000000000000000000000000000000000000000000000000000

My worry is why bitset always gives 64, zero for later 3. Interestingly "cout" for the actual values works as expected. 我担心的是,为什么bitset总是给出64,后面的3总是为零。有趣的是,实际值的“ cout”按预期工作。

If you look at the std::bitset constructor you will see that it either takes a string as argument, or an integer . 如果看一下std::bitset构造函数,您会发现它要么使用字符串作为参数,要么使用整数

That means your double value will be converted to an integer, and there is no standard integer type that can hold such large values, and that leads to undefined behavior . 这意味着您的double值将转换为整数,并且没有标准的整数类型可以容纳这么大的值,从而导致未定义的行为

If you want to get the actual bits of the double you need to do some casting tricks to make it work: 如果要获取double的实际位数,则需要执行一些强制转换技巧以使其起作用:

unsigned long long bits = *reinterpret_cast<unsigned long long*>(&doubleValue);

Note that type-punning like this is not defined in the C++ specification, but as long as sizeof(double) == sizeof(unsigned long long) it will work. 请注意,在C ++规范中未定义类似这种类型的类型转换 ,但是只要sizeof(double) == sizeof(unsigned long long)就会起作用。 If you want the behavior to be well-defined you have to go through arrays of char and char* . 如果您希望行为明确定义,则必须遍历charchar*数组。

With C++14, std::bitset now takes an unsigned long long constructor, so this might work: 在C ++ 14中, std::bitset现在需要一个unsigned long long构造函数,因此这可能起作用:

union udouble {
  double d;
  unsigned long long u;
};

void Display(double doubleValue)
{
    udouble ud;
    ud.d = doubleValue;
    bitset<sizeof(double) * 8> b(ud.u);
    cout << "Value  : " << doubleValue << endl;
    cout << "BitSet : " << b.to_string() << endl;
}

This should give you the internal representation of a double. 这应该为您提供双精度的内部表示。 See the working sample code on IdeOne . 请参阅IdeOne上的工作示例代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM