转换双保持msb? [英] convert a double keeping msb ?

查看:65
本文介绍了转换双保持msb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有问题,我有类似的DOUBLE值


4065 4000 0000 0000


我希望将它转换成这样的DWORD:

4065 4000


如果我做结果=(DWORD) MyDouble结束AA(170)

我不想要。


信不信由你,但我花了4个小时, />
我无法移位,编译器投诉双,

它说:坏左操作数" 坏右操作数虽然它可以和DWORD一起工作




我很困惑,任何帮助都很感激(VC ++ 5.0)


Dan

解决方案



" DanSteph" <沓****** @ caramail.com>在消息中写道

news:40 ********************** @ news.free.fr ...

大家好,

我有一个问题,我有一个像这样的DOUBLE值

4065 4000 0000 0000

我想要把它转换成这样的DWORD:
4065 4000
如果我做结果=(DWORD)MyDouble以AA结尾(170)
我不想要。

信不信由你,但我花了4个小时才知道这个,
我无法改变这一点,编译器抱怨双,
它说:坏左操作数 坏右操作数虽然它和DWORD一起工作很好。

我很困惑,任何帮助都很感激(VC ++ 5.0)

Dan




喜欢这个(这不是便携式代码)


double x = ...;

DWORD msb = *(DWORD * )& x;


如果不行则试试


DWORD msb = *((DWORD *)& x + 1);


在任何一种情况下,技巧都是通过指针进行转换。这避免了编译器将double值转换为DWORD值的




john


John哈里森写道:


DanSteph <沓****** @ caramail.com>在消息中写道
新闻:40 ********************** @ news.free.fr ...

你好所有,

我有一个问题,我有一个像这样的DOUBLE值

4065 4000 0000 0000

我希望将它转换为DWORD就是这样:
4065 4000
如果我做结果=(DWORD)MyDouble以AA结尾(170)
我不想要。

信不信由你,但我花了4个小时才知道这个,
我无法改变这一点,编译器抱怨双,
它说:坏左操作数 坏右操作数虽然它和DWORD一起工作很好。

我很困惑,任何帮助都很感激(VC ++ 5.0)

Dan



喜欢这个(这不是便携式代码)

双x = ......;
DWORD msb = *(DWORD *)& x;

如果那不起作用,请尝试

DWORD msb = *((DWORD *)& x + 1);




It是DWORD msb = *((DWORD *)& x + 1);在小端机器上

(例如PC)。


这个让我思考:是上面未定义的结果

或实现定义的行为(假设大小为双b / b和上面的DWORD)?有两件事我不确定

关于 :调整reinterpret_cast-ed指针并取消引用

结果值。据我所知,两者都没有明确表示宽恕。只是理论上的兴趣。


Denis


> >

喜欢这个(这不是便携式代码)

double x = ...;
DWORD msb = * (DWORD *)& x;

如果不起作用,请尝试

DWORD msb = *((DWORD *)& x + 1);



这是DWORD msb = *((DWORD *)& x + 1);在小端机器上(例如PC)。

这个让我思考:是上面未定义的
或实现定义的行为的结果(假设大小为double
和DWORD一样)?有两件事我不确定
关于 :调整reinterpret_cast-ed指针并取消引用结果值。据我所知,两者都没有明确的宽恕。只是理论上的兴趣。

丹尼斯




标准在解释时非常糟糕。我认为这是不确定的行为(两者都有)因为我已经看到了

其他人声称在这个群体中,我无法指出你

标准的部分支持。


标准明确允许复制到char数组,

所以如果OP关注UB那么他应该将双重复制到一个

数组的char并从中构造DWORD。


john


hello all,

I have a problem, I have a DOUBLE value like that

4065 4000 0000 0000

and I want to convert it to a DWORD like that:
4065 4000

if I do result=(DWORD)MyDouble it end with AA(170)
wich I don''t want.

Believe me or not but I spent 4 hours on this,
I cannot shift the bits, the compiler complaint with double,
it say: "bad left operand" "bad right operand" while it work
well with DWORD.

I''m puzzled, any help appreciated (VC++ 5.0)

Dan

解决方案


"DanSteph" <Da******@caramail.com> wrote in message
news:40**********************@news.free.fr...

hello all,

I have a problem, I have a DOUBLE value like that

4065 4000 0000 0000

and I want to convert it to a DWORD like that:
4065 4000

if I do result=(DWORD)MyDouble it end with AA(170)
wich I don''t want.

Believe me or not but I spent 4 hours on this,
I cannot shift the bits, the compiler complaint with double,
it say: "bad left operand" "bad right operand" while it work
well with DWORD.

I''m puzzled, any help appreciated (VC++ 5.0)

Dan



Like this (this is not portable code)

double x = ...;
DWORD msb = *(DWORD*)&x;

if that doesn''t work try

DWORD msb = *((DWORD*)&x + 1);

In either case the trick is doing the conversion via a pointer. That avoids
the compiler converting a double value to a DWORD value.

john


John Harrison wrote:


"DanSteph" <Da******@caramail.com> wrote in message
news:40**********************@news.free.fr...

hello all,

I have a problem, I have a DOUBLE value like that

4065 4000 0000 0000

and I want to convert it to a DWORD like that:
4065 4000

if I do result=(DWORD)MyDouble it end with AA(170)
wich I don''t want.

Believe me or not but I spent 4 hours on this,
I cannot shift the bits, the compiler complaint with double,
it say: "bad left operand" "bad right operand" while it work
well with DWORD.

I''m puzzled, any help appreciated (VC++ 5.0)

Dan



Like this (this is not portable code)

double x = ...;
DWORD msb = *(DWORD*)&x;

if that doesn''t work try

DWORD msb = *((DWORD*)&x + 1);



It is DWORD msb = *((DWORD*)&x + 1); on little endian machines
(e.g. PC).

This one got me thinking: is the result of the above undefined
or implementation-defined behaviour (assuming the size of double
and DWORD as above)? There are two things that I am unsure
about: adjusting a reinterpret_cast-ed pointer and dereferencing
the resulting value. As far as I know, neither is explicitly
condoned. Just a theoretical interest.

Denis


> >

Like this (this is not portable code)

double x = ...;
DWORD msb = *(DWORD*)&x;

if that doesn''t work try

DWORD msb = *((DWORD*)&x + 1);



It is DWORD msb = *((DWORD*)&x + 1); on little endian machines
(e.g. PC).

This one got me thinking: is the result of the above undefined
or implementation-defined behaviour (assuming the size of double
and DWORD as above)? There are two things that I am unsure
about: adjusting a reinterpret_cast-ed pointer and dereferencing
the resulting value. As far as I know, neither is explicitly
condoned. Just a theoretical interest.

Denis



Its the sort of thing that the standard is really bad at explaining. I
believe that it is undefined behaviour (on both counts) because I''ve seen
others claim that in this group, I couldn''t point you to the sections of the
standard to back that up.

Copying to an array of char is explicitly allowed by the standard however,
so if the OP was concerned about UB then he should copy the double to an
array of char and construct the DWORD from that.

john


这篇关于转换双保持msb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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