将float的位重新解释为unsigned long [英] Reinterpret the bits of a float as unsigned long

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

问题描述

对于哈希函数,我想将float

表达式的位重新解释为unsigned long。正常投射


(无符号长)float_expression


将浮点数截断为(无符号长整数)。

但这不是我想要的哈希函数的效果。

使用联合我的哈希函数可以实现:


union {

long unsigned hashvalue;

float floatvalue;

} value;


value.floatvalue = float_expression ;


现在


value.hashvalue


按预期包含重新解释的位。但我认为较短的

解决方案(没有赋值或memcpy变量)应该可以




当我演员首先到(void *)然后到(unsigned long)

gcc给我错误:


无法转换为指针类型


似乎gcc禁止向指针投射浮动。


有人有想法重新解释浮点的位置

unsigned long没有赋值或memcpy(某种类型

棘手的演员)。


问候Thomas Mertes


Seed7主页: http://seed7.sourceforge.net

维基百科: http://en.wikipedia.org/wiki/Seed7

项目页面: http://sourceforge.net/projects /秒eed7

解决方案

< th *********** @ gmx.at>在消息中写道

新闻:11 ********************** @ g47g2000cwa.googlegr oups.com ...

对于哈希函数,我想将float
表达式的位重新解释为unsigned long。正常演员

(unsigned long)float_expression

将float缩减为(unsigned long)整数。

但这不是效果我想要一个哈希函数。
使用联合我的哈希函数可以实现:

union {
long unsigned hashvalue;
float floatvalue;
} value ;

value.floatvalue = float_expression;

现在

value.hashvalue

包含重新解释的位。但是我认为一个较短的解决方案(没有赋值或memcpy到变量)应该是可能的。

当我首先转换为(void *)并之后转换为(unsigned)很长)
gcc给我的错误:

无法转换为指针类型

gcc似乎禁止向指针投射浮动。

有没有人想要重新解释一个float的位,如果没有赋值或memcpy(某种类型的棘手演员),那就是无符号长。




如果float_expression是左值,你可以写:


*(unsigned long *)&(float_expression)


但这充满了危险。


PJ Plauger

Dinkumware,Ltd。
http://www.dinkumware.com


" PJ Plauger" <,P ... @ dinkumware.com>写道:

如果float_expression是一个左值,你可以写:

*(unsigned long *)&(float_expression)

>但这充满了危险。




感谢您的帮助。但是float_expression不是

被授予左值。你有一个rvalue解决方案吗?


问候Thomas Mertes


Seed7主页: http://seed7.sourceforge.net

维基百科: http://en.wikipedia.org/wiki/Seed7

项目页面: http://sourceforge.net/projects/seed7


th *********** @ gmx.at 写道:

" PJ Plauger" <,P ... @ dinkumware.com>写道:

如果float_expression是一个左值,你可以写:

*(unsigned long *)&(float_expression)

>但这充满了危险。



感谢您的帮助。但是float_expression不被授予左值。你有一个rvalue解决方案吗?



你可以通过转换为

unsigned char *来获得基础表示,并通过它来完成。

unsigned char * raw =(unsigned char *)& floatval;

将raw [0]与raw [sizeof(float)-1]混合(例如构建一个

无符号长位......)


For a hash function I want to reinterpret the bits of a float
expression as unsigned long. The normal cast

(unsigned long) float_expression

truncates the float to an (unsigned long) integer.

But this is not the effect I want for a hash function.
With unions my hash function can be implemented:

union {
long unsigned hashvalue;
float floatvalue;
} value;

value.floatvalue = float_expression;

Now

value.hashvalue

contains the reinterpreted bits as intended. But I think a shorter
solution (without assignment or memcpy to a variable) should
be possible.

When I cast first to (void *) and after that to (unsigned long)
the gcc gives me the error:

cannot convert to a pointer type

It seems casting a float to a pointer is prohibited by gcc.

Has anybody an idea to reinterpret the bits of a float as
unsigned long without assignment or memcpy (some sort
of tricky cast).

Greetings Thomas Mertes

Seed7 Homepage: http://seed7.sourceforge.net
Wikipedia: http://en.wikipedia.org/wiki/Seed7
Project page: http://sourceforge.net/projects/seed7

解决方案

<th***********@gmx.at> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

For a hash function I want to reinterpret the bits of a float
expression as unsigned long. The normal cast

(unsigned long) float_expression

truncates the float to an (unsigned long) integer.

But this is not the effect I want for a hash function.
With unions my hash function can be implemented:

union {
long unsigned hashvalue;
float floatvalue;
} value;

value.floatvalue = float_expression;

Now

value.hashvalue

contains the reinterpreted bits as intended. But I think a shorter
solution (without assignment or memcpy to a variable) should
be possible.

When I cast first to (void *) and after that to (unsigned long)
the gcc gives me the error:

cannot convert to a pointer type

It seems casting a float to a pointer is prohibited by gcc.

Has anybody an idea to reinterpret the bits of a float as
unsigned long without assignment or memcpy (some sort
of tricky cast).



If float_expression is a lvalue, you can write:

*(unsigned long *)&(float_expression)

but that is fraught with peril.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


"P.J. Plauger" <p...@dinkumware.com> wrote:

If float_expression is a lvalue, you can write:

*(unsigned long *)&(float_expression)

but that is fraught with peril.



Thank you for your help. But the float_expression is not
granted to be an lvalue. Do you have an rvalue solution as well?

Greetings Thomas Mertes

Seed7 Homepage: http://seed7.sourceforge.net
Wikipedia: http://en.wikipedia.org/wiki/Seed7
Project page: http://sourceforge.net/projects/seed7


th***********@gmx.at wrote:

"P.J. Plauger" <p...@dinkumware.com> wrote:

If float_expression is a lvalue, you can write:

*(unsigned long *)&(float_expression)

but that is fraught with peril.



Thank you for your help. But the float_expression is not
granted to be an lvalue. Do you have an rvalue solution as well?


You can get at the underlying representation by casting to an
unsigned char* and work your way through that.
unsigned char *raw = (unsigned char *)&floatval;
Fiddle with raw[0] to raw[sizeof(float)-1] (e.g build an
unsigned long from the bits..)


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

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