title: How to store a *signed* 24 bit int into another variable?
body: <p>I need to encode a 24 bit integer into the end of 32 bit int. </p>
  
  <p>(the first byte contains other data, the other three are empty for use by the 24 bit int)</p>
  
  <p>I already have a <code>SET_BYTE</code> macro, and I can successfully do the following for <em>unsigned</em> 24 bit values:</p>
  
  <pre><code>SET_BYTE(DEST, START_BYTE_INDEX,   (uint8_t)(VALUE)); 
  SET_BYTE(DEST, START_BYTE_INDEX+1, (uint8_t)(VALUE &gt;&gt; 8)); 
  SET_BYTE(DEST, START_BYTE_INDEX+2, (uint8_t)(VALUE &gt;&gt; 16)); 
  </code></pre>
  
  <p>What I'm stuck on is that I'm not quite sure how to modify this approach to work for <strong>signed</strong> 24 bit integers?</p>
  
  <hr>
  
  <p>If I attempt to store the value -22 with the above (modified to use <code>int8_t</code>, obviously), for example, I get the following byte values:</p>
  
  <pre><code>-22
  -1
  -1
  </code></pre>
  
  <p>which read back as these (if I do <code>&lt;&lt;0</code>, <code>&lt;&lt;8</code> and <code>&lt;&lt;16</code> when reading):</p>
  
  <pre><code>-22
  -256
  -65536
  </code></pre>
  
  <p>I'm assuming I just need to totally avoid these shifts with signed values, but I'm not sure what the correct approach is?</p>