title: Why must I cast a `uint8_t` to `uint64_t` *before* left-shifting it?
body: <p>I just want to concatenate my uint8_t array to uint64_t. In fact, I solved my problem but need to understand the reason. Here is my code;</p>
  
  <pre><code>    uint8_t byte_array[5];
  
      byte_array[0] = 0x41;
      byte_array[1] = 0x42;
      byte_array[2] = 0x43;
      byte_array[3] = 0x44;
      byte_array[4] = 0x45;
  
      cout &lt;&lt; "index 0: " &lt;&lt; byte_array[0] &lt;&lt; " index 1: " &lt;&lt; byte_array[1] &lt;&lt; " index 2: " &lt;&lt; byte_array[2] &lt;&lt; " index 3: " &lt;&lt; byte_array[3] &lt;&lt; " index 4: " &lt;&lt; byte_array[4] &lt;&lt; endl;
  
      /* This does not work */
      uint64_t reverse_of_value = (byte_array[0] &amp; 0xff) | ((byte_array[1] &amp; 0xff) &lt;&lt; 8) | ((byte_array[2] &amp; 0xff) &lt;&lt; 16) | ((byte_array[3] &amp; 0xff) &lt;&lt; 24) | ((byte_array[4] &amp; 0xff) &lt;&lt; 32);
  
      cout &lt;&lt; reverse_of_value &lt;&lt; endl;
  
      /* this works fine */
      reverse_of_value = (uint64_t)(byte_array[0] &amp; 0xff) | ((uint64_t)(byte_array[1] &amp; 0xff) &lt;&lt; 8) | ((uint64_t)(byte_array[2] &amp; 0xff) &lt;&lt; 16) | ((uint64_t)(byte_array[3] &amp; 0xff) &lt;&lt; 24) | ((uint64_t)(byte_array[4] &amp; 0xff) &lt;&lt; 32);
  
      cout &lt;&lt; reverse_of_value &lt;&lt; endl;
  </code></pre>
  
  <p>The first output will be "44434245" and second one will be "4544434241" that is what I want.</p>
  
  <p>So as we see when I use casting each byte to uint64_t code works, however, if I do not use casting it gives me irrelevant result. Can anybody explain the reason?</p>