Fast multiplication/division by 2 for floats and doubles (C/C++)

This is one of those highly-application specific things. It may help in some cases and not in others. (In the vast majority of cases, a straight-forward multiplication is still best.) The “intuitive” way of doing this is just to extract the bits into a 64-bit integer and add the shift value directly into the exponent. … Read more

What’s the best C++ way to multiply unsigned integers modularly safely?

Some template metaprogramming with SFINAE, perhaps. #include <type_traits> template <typename T, typename std::enable_if<std::is_unsigned<T>::value && (sizeof(T) <= sizeof(unsigned int)) , int>::type = 0> T safe_multiply(T a, T b) { return (unsigned int)a * (unsigned int)b; } template <typename T, typename std::enable_if<std::is_unsigned<T>::value && (sizeof(T) > sizeof(unsigned int)) , int>::type = 0> T safe_multiply(T a, T b) { … Read more

Why do these two multiplication operations give different results?

long oneYearWithL = 1000*60*60*24*365L; long oneYearWithoutL = 1000*60*60*24*365; Your first value is actually a long (Since 365L is a long, and 1000*60*60*24 is an integer, so the result of multiplying a long value with an integer value is a long value. But 2nd value is an integer (Since you are mulitplying an integer value with … Read more

tech