What's a proper way of type-punning a float to an int and vice-versa?
The code below performs a fast inverse square root operation by some bit hacks. The algorithm was probably developed by Silicon Graphics in early 1990's and it's appeared in Quake 3 too. more info However I get the following warning from GCC C++ compiler : dereferencing type-punned pointer will break strict-aliasing rules Should I use static_cast , reinterpret_cast or dynamic_cast instead in such situations? float InverseSquareRoot(float x) { float xhalf = 0.5f*x; int32_t i = *(int32_t*)&x; i = 0x5f3759df - (i>>1); x = *(float*)&i; x = x*(1.5f - xhalf*x*x); return x; } Forget casts. Use memcpy