title: Why does shifting 0xff left by 24 bits result in an incorrect value?
body: <p>I would like to shift <code>0xff</code> left by 3 bytes and store it in a <code>uint64_t</code>, which should work as such:</p>
  
  <pre><code>uint64_t temp = 0xff &lt;&lt; 24;
  </code></pre>
  
  <p>This yields a value of <code>0xffffffffff000000</code> which is most definitely not the expected <code>0xff000000</code>.</p>
  
  <p>However, if I shift it by fewer than 3 bytes, it results in the correct answer.</p>
  
  <p>Furthermore, trying to shift 0x01 left by 3 bytes does work.</p>
  
  <p>Here's my output:</p>
  
  <pre><code>0xff shifted by 0 bytes: 0xff
  0x01 shifted by 0 bytes: 0x1
  0xff shifted by 1 bytes: 0xff00
  0x01 shifted by 1 bytes: 0x100
  0xff shifted by 2 bytes: 0xff0000
  0x01 shifted by 2 bytes: 0x10000
  0xff shifted by 3 bytes: 0xffffffffff000000
  0x01 shifted by 3 bytes: 0x1000000
  </code></pre>
  
  <p>With some experimentation, left shifting works up to 3 bits for each uint64_t up to 0x7f, which yields 0x7f000000. 0x80 yields 0xffffffff80000000.</p>
  
  <p>Does anyone have an explanation for this bizarre behavior? 0xff000000 certainly falls within the 2<sup>64</sup> - 1 limits of uint64_t.  </p>