如何对浮点数执行按位运算

我试过了:

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

我收到一个编译器错误,提示 & 的操作数不能是浮点类型.

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

当我这样做时:

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.

以下也是不允许的.

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

我不明白为什么 int 可以转换为 void* 而不是 float.

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

我这样做是为了解决 Stack Overflow 问题中描述的问题 如何使用遗传算法求解线性方程组?.

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

推荐答案

在语言层面,没有浮点数的按位运算"之类的东西.C/C++ 中的按位运算处理数字的值表示.并且浮点数的值表示在 C/C++ 中没有定义(无符号整数在这方面是一个例外,因为它们的移位被定义为就像它们存储在 2 的补码中一样).浮点数在值表示级别上没有位,这就是为什么您不能对它们应用按位运算的原因.

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++ (unsigned integers are an exception in this regard, as their shift is defined as-if they are stored in 2's complement). Floating point numbers don't have bits at the level of value-representation, which is why you can't apply bitwise operations to them.

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

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]

请不要尝试将 float 对象重新解释为 int 对象,正如其他答案所建议的那样.这没有多大意义,并且不能保证在遵循严格别名规则优化的编译器中工作.在 C++ 中检查内存内容的正确方法是将其重新解释为 [signed/unsigned] char 数组.

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

另请注意,从技术上讲,您不能保证系统上的浮点表示是 IEEE754(尽管在实践中,除非您明确允许它不是,然后仅针对 -0.0、±infinity 和 NaN).

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).

相关文章