title: Fast multiplication/division by 2 for floats and doubles (C/C++)
body: <p>In the software I'm writing, I'm doing millions of multiplication or division by 2 (or powers of 2) of my values. I would really like these values to be <code>int</code> so that I could access the bitshift operators </p>
  
  <pre><code>int a = 1;
  int b = a&lt;&lt;24
  </code></pre>
  
  <p>However, I cannot, and I have to stick with doubles. </p>
  
  <p>My question is : <strong>as there is a standard representation of doubles (sign, exponent, mantissa), is there a way to play with the exponent to get fast multiplications/divisions by a power of 2</strong>?</p>
  
  <p>I can even assume that the number of bits is going to be fixed (the software will work on machines that will always have 64 bits long doubles)</p>
  
  <p>P.S : And yes, the algorithm mostly does these operations only. This is the bottleneck (it's already multithreaded).</p>
  
  <p>Edit : Or am I completely mistaken and clever compilers already optimize things for me?</p>
  
  <hr>
  
  <p>Temporary results (with Qt to measure time, overkill, but I don't care): </p>
  
  <pre><code>#include &lt;QtCore/QCoreApplication&gt;
  #include &lt;QtCore/QElapsedTimer&gt;
  #include &lt;QtCore/QDebug&gt;
  
  #include &lt;iostream&gt;
  #include &lt;math.h&gt;
  
  using namespace std;
  
  int main(int argc, char *argv[])
  {
  QCoreApplication a(argc, argv);
  
  while(true)
  {
      QElapsedTimer timer;
      timer.start();
  
      int n=100000000;
      volatile double d=12.4;
      volatile double D;
      for(unsigned int i=0; i&lt;n; ++i)
      {
          //D = d*32;      // 200 ms
          //D = d*(1&lt;&lt;5);  // 200 ms
          D = ldexp (d,5); // 6000 ms
      }
  
      qDebug() &lt;&lt; "The operation took" &lt;&lt; timer.elapsed() &lt;&lt; "milliseconds";
  }
  
  return a.exec();
  }
  </code></pre>
  
  <p>Runs suggest that <code>D = d*(1&lt;&lt;5);</code> and <code>D = d*32;</code> run in the same time (200 ms) whereas <code>D = ldexp (d,5);</code> is much slower (6000 ms). I <strong>know</strong> that this is a micro benchmark, and that suddenly, my RAM has exploded because Chrome has suddenly asked to compute Pi in my back every single time I run <code>ldexp()</code>, so this benchmark is worth nothing. But I'll keep it nevertheless.</p>
  
  <p>On the other had, I'm having trouble doing <code>reinterpret_cast&lt;uint64_t *&gt;</code> because there's a <code>const</code> violation (seems the <code>volatile</code> keyword interferes)</p>