Thread: bitwise operations with double

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    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?

    Thanks for your help!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Image rotation (Bitwise operations?)
    By 127.0.0.1 in forum Game Programming
    Replies: 1
    Last Post: 05-29-2009, 12:45 PM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. bitwise operations
    By black_watch in forum C++ Programming
    Replies: 9
    Last Post: 03-24-2007, 04:48 AM
  5. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM