如何打印长双二进制重新presentation在电脑的内存? [英] How to print binary representation of a long double as in computer memory?
问题描述
我要打印一些原因长双
数字的二进制重新presentation。我想看到的确切格式,因为它保留在计算机的内存。
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.
我通过以下问题到了哪里取联盟
是解决方案。对于浮动
,备用数据类型是 unsigned int类型
因为二者都是32位的。对于双击
,这是 unsigned long int类型
因为二者都是64位的。但在的长双
,它是96位/有没有类似的内存相当于消费128位(取决于平台)。那么,什么将是解决办法?
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?
- Print在C ++ 浮点数的二进制重新presentation
- 双的二进制重新presentation
- 的确切的二进制重新presentation双
- How我做一个展示float或double的二进制重新presentation?
- Print binary representation of a float number in C++
- Binary representation of a double
- Exact binary representation of a double
- How do I display the binary representation of a float or double?
有被标记为问题C++ - ?!如何(使用COUT)一个号码存储在存储器中的方式进行打印
真的是什么呢?提到的问题谈到了整数,它接受的解决方案的问题是位集
刚刚截断浮点部分。我的主要观点是浮点再同该问题的内容没有任何关系presentation。
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.
推荐答案
一如既往,以别名的方式任意内存是 unsigned char型
组成的数组。期。所有这些联盟的解决方案具有不确定的行为,因而其实不是的解决方案任何责任。
As always, the way to alias arbitrary memory is with an array of unsigned char
s. Period. All those "union" solutions have undefined behaviour and are thus in fact not "solutions" whatsoever.
于是复制到一个 unsigned char型
阵,那么就打印出字节值一个接一个。
So copy into an unsigned char
array, then just print out the byte values one by one.
顺便说一句,长双
不一定是96位。它会依赖于平台。
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';
}
这篇关于如何打印长双二进制重新presentation在电脑的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!