1

I have to print the binary representation of a long double number for some reasons. I want to see the exact format as it remains in the computer memory.

I went through the following questions where taking a union was the the solution. For float, alternate datatype was unsigned int as both are 32-bit. For double, it was unsigned long int as both are 64-bit. But in of long double, it is 96-bit/128-bit (depending on platform) which has no similar equivalent memory consumer. So, what would be the solution?

Visited Questions:

  1. Print binary representation of a float number in C++
  2. Binary representation of a double
  3. Exact binary representation of a double
  4. How do I display the binary representation of a float or double?

ATTENTION:

It is marked as duplicate of the question How to print (using cout) the way a number is stored in memory? !

Really is it? The question mentioned question talked about integer number and accepted solution of it was bitset which just truncates the floating-point part. My main point of view is floating-point representation which has no relation with the content of that question.

16
  • 1
    An array of unsigned char, as in all other cases. Commented Jun 16, 2016 at 14:00
  • "I want to see the exact format as it remains in the computer memory." Look it up on Wikipedia. Commented Jun 16, 2016 at 14:00
  • @LightnessRacesinOrbit I know how it remains dividing in mantissa and exponent. So, I want to print these 96-bit in the screen, That means how to start the coding for it. I actually looking it for some other problem, where it seemed to me, binary to decimal conversion loosing some data. Thanks Commented Jun 16, 2016 at 14:04
  • 2
    @manetsus: Maybe you should present your minimal reproducible example for that problem, instead of fiddling about with floating-point binary representations? Commented Jun 16, 2016 at 14:07
  • 1
    Strange! how it could be the duplicate of C++ - How to print (using cout) the way a number is stored in memory? ? @Cody-gray Did you have a look on the content? Commented Jun 16, 2016 at 14:26

1 Answer 1

7

As always, the way to alias arbitrary memory is with an array of unsigned chars. Period. All those "union" solutions have undefined behaviour and are thus in fact not "solutions" whatsoever.

So copy into an unsigned char array, then just print out the byte values one by one.

Incidentally, long double is not necessarily 96-bit. It'll depend on the platform.

#include <iostream> #include <algorithm> int main() { const long double x = 42; unsigned char a[sizeof(long double)]; std::copy( reinterpret_cast<const unsigned char*>(&x), reinterpret_cast<const unsigned char*>(&x) + sizeof(long double), &a[0] ); std::cout << "Bytes: " << sizeof(long double) << "\nValues: "; std::cout << std::hex << std::showbase; for (auto el : a) { std::cout << +el << ' '; } std::cout << '\n'; } 

(live demo)

Sign up to request clarification or add additional context in comments.

10 Comments

Is there any direct way to print it in binary instead of std::hex? Thanks
Ow, please don't miss understand me. There is nothing wrong with your solution. But in memory, it remains binary. So, I am interested in seeing it in binary. I just asked that is there any shortcut tricks to represent it in binary as you are an expert in this field. Thanks.
@manetsus: You mean you want to see the value of each byte in a base-2 representation? Okay so just do that then. Surely you don't need me to handhold you through every possible combination of output representation?
@manetsus std::ostream does not support binary output, you may write a function that converts unsigned char to binary string which is trivial.
@manetsus to make it even worse - c++ does not guarantee that byte has 8 bits, but there is a very small chance you will meet a platform where it is not so.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.