The bit-wise operations can be implemented on floating point numbers using remainders (modulo) and inequality checks. The correct way to inspect memory content in C++ is by reinterpreting it as an array of char.Īlso 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).įWIW, there is a real use case for bit-wise operations on floating point (I just ran into it recently) - shaders written for OpenGL implementations that only support older versions of GLSL (1.2 and earlier did not have support for bit-wise operators), and where there would be loss of precision if the floats were converted to ints. That doesn't make much sense, and is not guaranteed to work in compilers that follow strict-aliasing rules in optimization. inspect memory from c to cĪnd please, don't try to reinterpret a float object as an int object, as other answers suggest.
![cutviewer mill invalid floating point operation cutviewer mill invalid floating point operation](https://i.ytimg.com/vi/oddASsFwZp0/maxresdefault.jpg)
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 Floating point numbers don't have bits at the level of value-representation, which is why you can't apply bitwise operations to them.Īll you can do is analyze the bit content of the raw memory occupied by the floating-point 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).
![cutviewer mill invalid floating point operation cutviewer mill invalid floating point operation](https://i.ytimg.com/vi/5WN2zs0XJWc/maxresdefault.jpg)
Bitwise operations in C/C++ work on value-representation of a number. At the language level, there's no such thing as "bitwise operation on floating-point numbers".