title: C/C++ - Convert 24-bit signed integer to float
body: <p>I'm programming in C++. I need to convert a 24-bit signed integer (stored in a 3-byte array) to float (normalizing to [-1.0,1.0]).</p>
  
  <p>The platform is MSVC++ on x86 (which means the input is little-endian).</p>
  
  <p>I tried this:</p>
  
  <pre><code>float convert(const unsigned char* src)
  {
      int i = src[2];
      i = (i &lt;&lt; 8) | src[1];
      i = (i &lt;&lt; 8) | src[0];
  
      const float Q = 2.0 / ((1 &lt;&lt; 24) - 1.0);
  
      return (i + 0.5) * Q;
  }
  </code></pre>
  
  <p>I'm not entirely sure, but it seems the results I'm getting from this code are incorrect. So, is my code wrong and if so, why?</p>