title: Sign extension from 16 to 32 bits in C
body: <p>I have to do a sign extension for a 16-bit integer and for some reason, it seems not to be working properly. Could anyone please tell me where the bug is in the code? I've been working on it for hours.</p>
  
  <pre><code>int signExtension(int instr) {
      int value = (0x0000FFFF &amp; instr);
      int mask = 0x00008000;
      int sign = (mask &amp; instr) &gt;&gt; 15;
      if (sign == 1)
          value += 0xFFFF0000;
      return value;
  }
  </code></pre>
  
  <p>The instruction (instr) is 32 bits and inside it I have a 16bit number.</p>