title: What is going on with bitwise operators and integer promotion?
body: <p>I have a simple program. <strong><em>Notice that I use an unsigned fixed-width integer 1 byte in size.</em></strong></p>
  
  <pre><code>#include &lt;cstdint&gt;
  #include &lt;iostream&gt;
  #include &lt;limits&gt;
  
  int main()
  {
      uint8_t x = 12;
      std::cout &lt;&lt; (x &lt;&lt; 1) &lt;&lt; '\n';
      std::cout &lt;&lt; ~x;
  
      std::cin.clear();
      std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
      std::cin.get();
  
      return 0;
  }
  </code></pre>
  
  <p>My output is the following.</p>
  
  <pre><code>24
  -13
  </code></pre>
  
  <p><em>I tested larger numbers and operator <code>&lt;&lt;</code> always gives me positive numbers, while operator <code>~</code> always gives me negative numbers. I then used <code>sizeof()</code> and found...</em></p>
  
  <blockquote>
    <p><em>When I use the left shift bitwise operator(<code>&lt;&lt;</code>), I receive an unsigned 4 byte integer.</em></p>
    
    <p><em>When I use the bitwise not operator(<code>~</code>), I receive a signed 4 byte integer.</em></p>
  </blockquote>
  
  <p>It seems that the bitwise not operator(<code>~</code>) does a signed integral promotion like the arithmetic operators do. However, the left shift operator(<code>&lt;&lt;</code>) seems to promote to an unsigned integral.</p>
  
  <p>I feel obligated to know when the compiler is changing something behind my back. If I'm correct in my analysis, do all the bitwise operators promote to a 4 byte integer? And why are some signed and some unsigned? I'm so confused!</p>
  
  <p><strong>Edit:</strong> My assumption of always getting positive or always getting negative values was wrong. But from being wrong, I understand what was really happening thanks to the great answers below.</p>