title: Sign extend 11 bits to 32 bits
body: <p>How do you sign extend an 11 bit number to a 32 bit number in C++?</p>
  
  <p>I have the following function, but it is not returning the proper sign-extension as expected:</p>
  
  <pre><code>unsigned int signExtend11to32ui(int i) {
      int mask = 2047; // 0x7FF or 0000 0111 1111 1111
      return static_cast&lt;unsigned int&gt;(static_cast&lt;int&gt;(i &amp; mask));
  }
  </code></pre>
  
  <p>The following function works correctly for sign-extension of 16 bits to 32 bits:</p>
  
  <pre><code>unsigned int signExtend16to32ui(short i) {
      return static_cast&lt;unsigned int&gt;(static_cast&lt;int&gt;(i));
  }
  </code></pre>