Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6938173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:27:31+00:00 2026-05-27T12:27:31+00:00

Possible Duplicate: Float to binary in C++ I have a very small double var,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:27:32+00:00Added an answer on May 27, 2026 at 12:27 pm
    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";
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why can't decimal numbers be represented exactly in binary? i get very
Possible Duplicate: Float to binary in C++ I want to print out the binary
Possible Duplicate: Should I use double or float ? When would I rather use
Possible Duplicate: Most effective way for float and double comparison I am new to
Possible Duplicate: convert seconds to HH-MM-SS with javascript ? Hi, I have a float
Possible Duplicate: Python rounding error with float numbers I have a rounding Problem in
Possible Duplicate: Why not use Double or Float to represent currency? I'm writing a
Possible Duplicate: Double value to round up in Java I am getting float number
Possible Duplicate: JavaScript: formatting number with exactly two decimals Now that I have got
Possible Duplicate: Why doesn't C have unsigned floats? The question is probably very basic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.