reinterpret_cast - 将double解释为long [英] reinterpret_cast - to interpret double as long

查看:85
本文介绍了reinterpret_cast - 将double解释为long的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将double转换为二进制表示。我可以使用

"&"使用位掩码的位操作将*非*浮点类型转换为二进制

表示,但我不能使用&双打。


为了解决这个双倍的限制,我想保持

的两倍*相同*但改变它解释很久。我可以使用

"&"在多头。我试图使用reinterpret_cast来达到这个目的,但它每次都是
返回零。


double n1 = 32;

long n2 = *(reinterpret_cast< long *>(& n)); //返回零,对于所有n1


***如何将双倍的位保持为*相同*但将其

解释更改为long ?我做错了什么?


谢谢,

Suzanne

I''d like to convert a double to a binary representation. I can use the
"&" bit operation with a bit mask to convert *non* float types to binary
representations, but I can''t use "&" on doubles.

To get around this limitation on double, I''d like to keep the bits of
the double the *same* but change its interpretation to long. I can use
"&" on longs. I tried to use reinterpret_cast for this purpose, but it
returned zero every time.

double n1 = 32;
long n2 = *(reinterpret_cast<long*>(&n)); // returns zero, for all n1

*** How can I keep the bits of the double the *same* but change its
interpretation to long? What am I doing wrong?

Thanks,
Suzanne

推荐答案

你想要位模式还是值???


" Suzanne Vogel" <苏************* @ hotmail.com>在消息中写道

新闻:3f ********** @ news.unc.edu ...
Do you want the bit pattern or the value???

"Suzanne Vogel" <su*************@hotmail.com> wrote in message
news:3f**********@news.unc.edu...
我想转换一个双到二进制表示。我可以使用
"&"使用位掩码进行位操作以将*非*浮点类型转换为二进制
表示,但我不能使用&关于double的这个限制,我想保持
的两倍*相同*但是将它的解释改为long。我可以使用
"&"在多头。我试图为此目的使用reinterpret_cast,但它每次都返回零。

double n1 = 32;
long n2 = *(reinterpret_cast< long *>(& ; N)); //返回零,对于所有n1

***如何保持双倍的* *相同*但将其解释更改为long?我做错了什么?

谢谢,
Suzanne
I''d like to convert a double to a binary representation. I can use the
"&" bit operation with a bit mask to convert *non* float types to binary
representations, but I can''t use "&" on doubles.

To get around this limitation on double, I''d like to keep the bits of
the double the *same* but change its interpretation to long. I can use
"&" on longs. I tried to use reinterpret_cast for this purpose, but it
returned zero every time.

double n1 = 32;
long n2 = *(reinterpret_cast<long*>(&n)); // returns zero, for all n1

*** How can I keep the bits of the double the *same* but change its
interpretation to long? What am I doing wrong?

Thanks,
Suzanne



" Suzanne Vogel" <苏************* @ hotmail.com>写道...
"Suzanne Vogel" <su*************@hotmail.com> wrote...
我想将double转换为二进制表示。我可以使用
"&"使用位掩码进行位操作以将*非*浮点类型转换为二进制
表示,但我不能使用&关于double的这个限制,我想保持
的两倍*相同*但是将它的解释改为long。我可以使用
"&"在多头。我试图为此目的使用reinterpret_cast,但它每次都返回零。

double n1 = 32;
long n2 = *(reinterpret_cast< long *>(& ; N)); //返回零,对于所有n1

***如何保持双倍的* *相同*但将其解释更改为long?我做错了什么?
I''d like to convert a double to a binary representation. I can use the
"&" bit operation with a bit mask to convert *non* float types to binary
representations, but I can''t use "&" on doubles.

To get around this limitation on double, I''d like to keep the bits of
the double the *same* but change its interpretation to long. I can use
"&" on longs. I tried to use reinterpret_cast for this purpose, but it
returned zero every time.

double n1 = 32;
long n2 = *(reinterpret_cast<long*>(&n)); // returns zero, for all n1

*** How can I keep the bits of the double the *same* but change its
interpretation to long? What am I doing wrong?




在你的平台上''double''可能比''long''长。并且

您的硬件很可能是小端的。 ''32''作为一个双尾的

零尾数。这意味着只有UPPER两个字节有一些非零位。对于任何2的幂的双倍,低六个字节都是零

。当你要求双倍的

地址时,你会得到最低字节的地址。

将它重新解释为一个长的地址会给你四个较低的

字节。它们没有设置位。


你需要至少两个多头才能在平台上看到双b / b
的位模式。尝试这样做(非便携式!):


double d1 = 32;

unsigned long * plong = reinterpret_cast< unsigned long *>(& d1);

unsigned long n1 = plong [0],n2 = plong [1];


并查看你在n1和n2中得到的结果。 (我使用unsigned long因为

它们对于位模式更好)


再一次:这段代码与标准C ++无关。它

产生_undefined_behaviour_。无论在

上做什么,你的平台都不是它应该在任何其他平台上做的事情

(甚至是同一编译器的不同版本)。


Victor



On your platform ''double'' is likely longer than ''long''. And
your hardware is likely little-endian. ''32'' as a double has
zero mantissa. That means that only the UPPER two bytes have
some non-zero bits in them. The lower six bytes are all zeroes
for any double that is a power of two. When you ask for the
address of the double, you get the address of the lowest byte.
Reinterpreting it as an address of a long gives you four lower
bytes. They have no bits set in them.

You need at least two longs to see the bit pattern in a double
on your platform. Try doing this (non-portable!):

double d1 = 32;
unsigned long *plong = reinterpret_cast<unsigned long*>(&d1);
unsigned long n1 = plong[0], n2 = plong[1];

and see what you get in n1 and n2. (I used unsigned long because
they are better for bit patterns)

Once again: this code has nothing to do with standard C++. It
produces _undefined_behaviour_. Whatever it happens to do on
your platform is not what it should do on any other platform
(or even with a different version of the same compiler).

Victor




" Suzanne Vogel" <苏************* @ hotmail.com>在消息中写道

新闻:3f ********** @ news.unc.edu ...

"Suzanne Vogel" <su*************@hotmail.com> wrote in message
news:3f**********@news.unc.edu...
我想转换一个双到二进制表示。我可以使用
"&"使用位掩码进行位操作以将*非*浮点类型转换为二进制
表示,但我不能使用&关于double的这个限制,我想保持
的两倍*相同*但是将它的解释改为long。我可以使用
"&"在多头。我试图为此目的使用reinterpret_cast,但它每次都返回零。

double n1 = 32;
long n2 = *(reinterpret_cast< long *>(& ; N)); //返回零,对于所有n1

***如何保持双倍的* *相同*但将其解释更改为long?我究竟做错了什么?


长度和double的大小差不多。我猜想

你只看到你的一半,而那一半都是零。


你可以试试这个

double n1 = 32;

long n2 = *(reinterpret_cast< long *>(& n));

long n3 = *(reinterpret_cast< ; long *>(& n)+ 1);


但更可靠的方法是使用工会


union

{

双d;

char c [sizeof(double)];

} conv;


conv.d = 32;

//以十六进制打印双倍

for(int i = 0; i< sizeof (double); ++ i

cout<< hex<< setfill(''0'')<< setw(2)<< conv.c [i ];


谢谢,
Suzanne
I''d like to convert a double to a binary representation. I can use the
"&" bit operation with a bit mask to convert *non* float types to binary
representations, but I can''t use "&" on doubles.

To get around this limitation on double, I''d like to keep the bits of
the double the *same* but change its interpretation to long. I can use
"&" on longs. I tried to use reinterpret_cast for this purpose, but it
returned zero every time.

double n1 = 32;
long n2 = *(reinterpret_cast<long*>(&n)); // returns zero, for all n1

*** How can I keep the bits of the double the *same* but change its
interpretation to long? What am I doing wrong?
Its pretty unlikely that long is the same size as double. I would guess that
you are only seeing half your double, and that half is all zero.

You could try this

double n1 = 32;
long n2 = *(reinterpret_cast<long*>(&n));
long n3 = *(reinterpret_cast<long*>(&n) + 1);

But a more reliable method would be to use a union

union
{
double d;
char c[sizeof(double)];
} conv;

conv.d = 32;
// print out double in hex
for (int i =0; i < sizeof(double); ++i
cout << hex << setfill(''0'') << setw(2) << conv.c[i];


Thanks,
Suzanne




john



john


这篇关于reinterpret_cast - 将double解释为long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆