title: How to make multiplication with bit shift in C#
body: <p>I know that bit shift multiplication used in operations of fixed point math , for example if i need to multiply two float values i should multiply it on scale factor (for example in that case 20) and after that, i should multiply result values as integer values and after that i should return they to normal presentation of numbers, should divide again on scale factor how to perform that with bit shift operations? </p>
  
  <p>Based on <a href="http://dspace.mah.se/bitstream/handle/2043/13691/exarbete%20Sebastian%20Holgersson%202012-04-25.pdf?sequence=2" rel="nofollow">this</a> article :  5.4 Fixed-point arithmetic</p>
  
  <p>I have tried this code example below, and i expected that result <code>floatResShift</code> and <code>floatResNormal</code> would be same but they are different, what i'm doing is wrong?:</p>
  
  <pre><code>        float mul1 = 18.579434f;
          float mul2 = 34.307951f;
  
          int shiftMul1 = (int)((2 ^ 32) * mul1);
          int shiftMul2 = (int)((2 ^ 32) * mul2);
  
          var resultMul = shiftMul1 * shiftMul2;
          float floatResShift = resultMul &gt;&gt; 32; // wrong value
          float floatResNormal = mul1 * mul2; //expected value
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>fixed point arithmetic explanation:</p>
  
  <blockquote>
    <p>Using ﬁxed-point arithmetic to calculate the result of A · B when A =
    2.5 and B = 8.4 using 32-bit integers would involve the following operations:</p>
    
    <p>Decide upon a scaling factor. This depends largely upon what kind of
    numbers are likely to be seen. As the numbers in this example are so
    low, it is less important, and 16 fractional bits (bits to the right
    of the radix point) are acceptable. The scaling factor will then be f
    = 216 = 65536. This format is known as Q15.16 (15 bits to the left of the radix point, 16 to the right and one bit for a sign).</p>
    
    <p>Multiply Ai and Bi using normal integer multiplication. Ri = Ai · Bi = 163840 · 550502 =
    90194247680. The reason for such a large number is that both Ai and Bi were scaled into our
    Q15.16 format, so the number that results from the multiplication is essentially (A · f) · (B · f) =
    A · B · f2.</p>
    
    <p>In order to bring our result back into the Q15.16 format, the result
    must thus be divided by the scaling factor. This too can be done using
    bit shift arithmetic, but for simplicity’s sake division is used here.
    Ri/f = 90194247680/65536 = 1376255 which is our result in Q15.16
    format</p>
    
    <p>To turn the number back into a normal real number, one only needs to
    cast it into the format desired and divide by the scaling factor
    again, so: 1376255.0/65536.0 = 20.999985 which is near the expected
    number 21.</p>
    
    <p>Scale numbers with the scaling factor. In binary arithmetic this can
    be accomplished using bit shifts, but for simplicity we will use
    multiplication by the scaling factor. Ai = A·f = 2.5·65536 = 163840
    and B · f = 8.4 · 65536 = 550502.4 which is then truncated turn it
    into an integer, so Bi = 550502.</p>
    
    <p>To turn the number back into a normal real number, one only needs to
    cast it into the format desired and divide by the scaling factor
    again, so: 1376255.0/65536.0 = 20.999985 which is near the expected
    number 21.</p>
  </blockquote>
  
  <p><strong>How to make the same like in comment above but with bit shift. And with big float values after point</strong></p>
  
  <p>I have tried with code above, but with no luck.</p>
  
  <p>For example i need to multiply two values 18.579434f and 34.307951f but using fixed point arithmetic.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I have tried this with less scale factor but with no luck.</p>
  
  <p><strong>SOLUTION:</strong></p>
  
  <p>Maybe i don't clearly explained the question, but i fix the problem and i found a solution:</p>
  
  <p>Thanks for all, question is closed, here is complete code with fixed point multiplication:</p>
  
  <pre><code>    float mul1 = 18.579434f;
      float mul2 = 34.307951f;
  
      int scaleFactor = (int) Math.Pow(2, 20);
  
      long shiftMul1 = (int)((scaleFactor) * mul1);
      long shiftMul2 = (int)((scaleFactor) * mul2);
  
      var resultMul = shiftMul1 * shiftMul2;
      float floatResShift = resultMul &gt;&gt; 40; 
      float floatResNormal = mul1 * mul2; // the result floatResNormal almost same as floatResShift
  </code></pre>