5

Possible Duplicate:
Float to binary in C++

I have a very small double var, and when I print it I get -0. (using C++). Now in order to get better precision I tried using

cout.precision(18); \\i think 18 is the max precision i can get.
cout.setf(ios::fixed,ios::floatfield);
cout<<var;\\var is a double.

but it just writes -0.00000000000...

I want to see the exact binary representation of the var.

In other words I want to see what binary number is written in the stack memory/register for this var.

Community
  • 1
  • 1
user690936
  • 985
  • 5
  • 13
  • 23

3 Answers3

8
union myUnion {
    double dValue;
    uint64_t iValue;
};

myUnion myValue;
myValue.dValue=123.456;
cout << myValue.iValue;

Update:

The version above will work for most purposes, but it assumes 64 bit doubles. This version makes no assumptions and generates a binary representation:

    double someDouble=123.456;
    unsigned char rawBytes[sizeof(double)];

    memcpy(rawBytes,&someDouble,sizeof(double));

    //The C++ standard does not guarantee 8-bit bytes
    unsigned char startMask=1;
    while (0!=static_cast<unsigned char>(startMask<<1)) {
        startMask<<=1;
    }

    bool hasLeadBit=false;   //set this to true if you want to see leading zeros

    size_t byteIndex;
    for (byteIndex=0;byteIndex<sizeof(double);++byteIndex) {
        unsigned char bitMask=startMask;
        while (0!=bitMask) {
            if (0!=(bitMask&rawBytes[byteIndex])) {
                std::cout<<"1";
                hasLeadBit=true;
            } else if (hasLeadBit) {
                std::cout<<"0";
            }
            bitMask>>=1;
        }
    }
    if (!hasLeadBit) {
        std::cout<<"0";
    }
IronMensan
  • 6,761
  • 1
  • 26
  • 35
6

This way is guaranteed to work by the standard:

double d = -0.0;
uint64_t u;
memcpy(&u, &d, sizeof(d));
std::cout << std::hex << u;
zr.
  • 7,528
  • 11
  • 50
  • 84
  • 1
    Guaranteed provided `sizeof(double) == sizeof(uint64_t)`, of course. And it's a pretty safe guess that it is. Also provided `uint64_t` exists in the implementation, which is reasonably safe but you have to provide the header that defines it yourself in older versions of MSVC. – Steve Jessop Dec 15 '11 at 14:55
  • 1
    thankyou for the answer, but this gives me a hex representation, i need a binary.. – user690936 Dec 15 '11 at 15:10
  • @user690936: each hex digit represents 4 bits, it's probably best to learn to read hex. But you could write a loop to print 1 bit at a time as `0`/`1`, or you could use calc.exe (on Windows) to convert. – Steve Jessop Dec 15 '11 at 17:21
  • @user690936 - I think the question they were answering is, what does a double look like written in a binary file. You may have been trying to ask, how do you convert a double to base 2. – sf_jeff Apr 22 '15 at 00:12
1

Try:

printf("0x%08x\n", myFloat);

This should work for a 32 bit variable, to display it in hex. I've never tried using this technique to see a 64 bit variable, but I think it's:

printf("%016llx\n", myDouble);

EDIT: tested the 64-bit version and it definitely works on Win32 (I seem to recall the need for uppercase LL on GCC.. maybe)

EDIT2: if you really want binary, you are best off using one of the other answers to get a uint64_t version of your double, and then looping:

for ( int i = 63; i >= 0; i-- )
{
    printf( "%d", (myUint64 >> i ) & 1 );
}
Hybrid
  • 396
  • 4
  • 20
  • thank you for the response. it does not give a binary number. for the number 2.0 it writes: 0x00000000 it should out put 10, no? – user690936 Dec 15 '11 at 14:51
  • I just tried it on my PC and it worked as expected. This code prints in Hexadecimal, not in binary. Also, your comment suggests that you don't understand the difference between an integer representation of 2, and a floating point version. an int with the value 2 has a binary representation of 10, but a float is totally different – Hybrid Dec 15 '11 at 15:09
  • i know that there is difference between an integer representation of 2, and a floating point version. but i still need a binary representation. a representation of {0,1} – user690936 Dec 15 '11 at 15:26