bitwise operations with double

• 10-03-2007
henry_kay
bitwise operations with double
Hi, i want to use bitwise operations in my code to perform fast multiplications. As far as i know, bitwise multiplications can be done only with integers but i am using bigger data, so is it possible to perform bitwise multiplications between doubles?

• 10-03-2007
Salem
> Hi, i want to use bitwise operations in my code to perform fast multiplications.
Most modern compilers will generate the optimal shift/add sequences to replace common multiplications for you, without you having to obfuscate the code in the process.

> so is it possible to perform bitwise multiplications between doubles?
Nope (or rather with the kind of difficulty which doesn't make it worthwhile).

Are you using a machine with a floating point co-processor?
For some code, some floating point operations are essentially 'free' in the sense that the main processor can do other work between starting a calculation and getting the result.
Smart compilers will re-arrange the code to exploit this.
• 10-03-2007
matsp
When you say "bitwise operations" and "multiply", it makes me think of shift operations - is this what you mean too?

In that case, no, shift operations on a double will not do the right thing, for several reasons, the main one being that the reason a double can hold a bigger data value than you could store in the same number of bits is that it's got the value stored as two parts: an exponent part and a mantissa part.
the number is stored is 2^exp * mantissa. So shifting "the whole lot" will not work.

If you really want to get "tricky" (and probably completely upset anyone reading the code, and make the floating point unit completely confused), you could add/subtract your "shift" from the exponent to have the same sort of effect - but you'd have to make sure you do that in a sensible way and deal with special cases such as zero, negative zero, not-a-number and what have you). And it's unlikely that it will be significantly faster to do it this way (if you are also going to do other math operations nearby), because passing a number back and forth between the integer unit and the floating point unit is also quite expensive in clock-cycles on any modern processor.

--
Mats