如何对浮点数执行按位操作 [英] How to perform a bitwise operation on floating point numbers

查看:865
本文介绍了如何对浮点数执行按位操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过这样:

  float a = 1.4123; 
a = a& (1 <3);

我得到一个编译器错误,说明& 不能是float类型。



当我这样做:

  float a = 1.4123; 
a =(int)a& (1 <3);

我得到程序运行。唯一的是,按位操作是在四舍五入后得到的数字的整数表示上完成的。



以下是不允许的。

  float a = 1.4123; 
a =(void *)a& (1 <3);

我不明白为什么 int 可以转换为 void * ,但不能转换为 float



我这样做是为了解决堆栈溢出问题描述的问题 如何使用遗传算法求解线性方程?

解决方案

没有诸如对浮点数的按位操作这样的事情。 C / C ++中的按位运算处理数字的值表示。浮点数的值表示在C / C ++中没有定义。浮点数在值表示级别上没有位,这就是为什么不能对它们应用按位操作。



所有你可以做的是分析原始内存的位内容由浮点数占用。为此,你需要使用如下建议的联合或者(等效地,并且只在C ++中)将浮点对象重新解释为 unsigned char 对象的数组,如

  float f = 5; 
unsigned char * c = reinterpret_cast< unsigned char *>(& f);
//检查从c [0]到c [sizeof f - 1]的内存

请不要尝试将 float 对象重新解释为 int 对象,因为其他答案建议。这没有什么意义,这是非法的,这不能保证在编译器中遵循严格的混叠规则在优化中工作。在C ++中检查内存内容的唯一合法方法是将其重新解释为 [signed / unsigned] char 的数组。

还要注意,你在技术上并不保证你的系统上的浮点表示是IEEE754(虽然在实践中它是除非你明确允许它不是,然后只是相对于-0.0,±无穷大和NaN )。


I tried this:

float a = 1.4123;
a = a & (1 << 3);

I get a compiler error saying that the operand of & cannot be of type float.

When I do:

float a = 1.4123;
a = (int)a & (1 << 3);

I get the program running. The only thing is that the bitwise operation is done on the integer representation of the number obtained after rounding off.

The following is also not allowed.

float a = 1.4123;
a = (void*)a & (1 << 3);

I don't understand why int can be cast to void* but not float.

I am doing this to solve the problem described in Stack Overflow question How to solve linear equations using a genetic algorithm?.

解决方案

At the language level, there's no such thing as "bitwise operation on floating-point numbers". Bitwise operations in C/C++ work on value-representation of a number. And the value-representation of floating point numbers is not defined in C/C++. Floating point numbers don't have bits at the level of value-representation, which is why you can't apply bitwise operations to them.

All you can do is analyze the bit content of the raw memory occupied by the floating-point number. For that you need to either use a union as suggested below or (equivalently, and only in C++) reinterpret the floating-point object as an array of unsigned char objects, as in

float f = 5;
unsigned char *c = reinterpret_cast<unsigned char *>(&f);
// inspect memory from c[0] to c[sizeof f - 1]

And please, don't try to reinterpret a float object as an int object, as other answers suggest. That doesn't make much sense, that is illegal, and that is not guaranteed to work in compilers that follow strict-aliasing rules in optimization. The only legal way to inspect memory content in C++ is by reinterpreting it as an array of [signed/unsigned] char.

Also note that you technically aren't guaranteed that floating-point representation on your system is IEEE754 (although in practice it is unless you explicitly allow it not to be, and then only with respect to -0.0, ±infinity and NaN).

这篇关于如何对浮点数执行按位操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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