title: Java 24-bit number signing
body: <p>I'm currently doing some work that has me working with some 24-bit integers.</p>
  
  <p>Essentially I need to be able to get both the signed, and unsigned values from these 24-bits.</p>
  
  <p>Currently I'm using the following code to put the three bytes together and return me their value.</p>
  
  <pre><code>private static int bytesToInt(byte[] input) {
      if (input.length == 3) {
          return (input[2] &amp; 0xFF) &lt;&lt; 16 | (input[1] &amp; 0xFF) &lt;&lt;8 | (input[0] &amp; 0xFF);
      }
  }
  </code></pre>
  
  <p>The input I'm giving it are the bytes: <code>0x42 0x87 0xfe</code> and the returned result is: <code>16680770</code></p>
  
  <p>This (I believe) is the correct unsigned value, however I also need the signed value of it, which I think is <code>-96446</code></p>
  
  <p>I'm guessing I'll have to do some bitshifting here to solve it, but I'm not sure how to accomplish this.</p>
  
  <p>I've tried casting the result into an and a long, but neither return the signed value.  I've also tried Math.abs(result), but I don't think I'm using that correctly.</p>
  
  <p>Any input would be appreciated.</p>
  
  <p>Thanks</p>