title: Is left-shifting (<<) a negative integer undefined behavior in C++11?
body: <p>Is left-shifting a negative <code>int</code> Undefined Behavior in C++11?</p>
  
  <p>The relevant Standard passages here are from 5.8:</p>
  
  <blockquote>
    <p>2/The value of E1 &lt;&lt; E2 is E1 left-shifted E2 bit positions; vacated
    bits are zero-filled. If E1 has an unsigned type, the value of the
    result is E1 × 2E2, reduced modulo one more than the maximum value
    representable in the result type. Otherwise, if E1 has a signed type
    and non-negative value, and E1×2E2 is representable in the result
    type, then that is the resulting value; otherwise, the behavior is
    undefined.</p>
  </blockquote>
  
  <p>The part that confuses me is:</p>
  
  <blockquote>
    <p>Otherwise, if E1 has a signed type and non-negative value, and E1×2E2
    is representable in the result type, then that is the resulting value;
    otherwise, the behavior is undefined.</p>
  </blockquote>
  
  <p>Should this be interpreted to mean that left-shifting <em>any</em> negative number is UB?  Or does it only mean <em>if</em> you LS a negative and the result doesn't fit in the result type, <em>then</em> it's UB?</p>
  
  <p>Moreover, the preceding clause says:</p>
  
  <blockquote>
    <p>1/The shift operators &lt;&lt; and >> group left-to-right.
        shift-expression:
            additive-expression
            shift-expression &lt;&lt; additive-expression
            shift-expression >> additive-expression</p>
    
    <p>The operands shall be of integral or unscoped enumeration type and
    integral promotions are performed.</p>
    
    <p>The type of the result is that of the promoted left operand. The
    behavior is undefined if the right operand is negative, or greater
    than or equal to the length in bits of the promoted left operand.</p>
  </blockquote>
  
  <p>This makes it explicit that using a negative number for <em>one</em> of the operands is UB.  If it were UB to use a negative for the other operand, I would expect that to be made clear here as well.</p>
  
  <p>So, bottom line, is:</p>
  
  <pre><code>-1 &lt;&lt; 1
  </code></pre>
  
  <p>Undefined Behavior?</p>
  
  <hr>
  
  <p>@Angew <a href="https://stackoverflow.com/a/19594249/241536">provided</a> a psudocode interpretation of the Standardese which succinctly expresses one possible (likely) valid interpretation.  Others have questioned whether this question is really about the applicability of the language "behavior is undefined" versus our (StackOverflow's) use of the phrase "Undefined Behavior."  This edit is to provide some more clarification on what I'm trying to ask.</p>
  
  <p>@Angew's interpretation of the Standardese is:</p>
  
  <pre><code>if (typeof(E1) == unsigned integral)
    value = E1 * 2^E2 % blah blah;
  else if (typeof(E1) == signed integral &amp;&amp; E1 &gt;= 0 &amp;&amp; representable(E1 * 2^E2))
    value = E1 * 2^E2;
  else
    value = undefined;
  </code></pre>
  
  <p>What this question really boils down to is this -- is the correct interpretation actually:</p>
  
  <pre><code>value = E1 left-shift-by (E2)
  
  switch (typeof(E1))
  {
  case unsigned integral :
    value = E1 * 2^E2 % blah blah;
    break;
  
  case signed integral :
    if (E1 &gt;= 0)
    { 
      if (representable(E1 * 2^E2))
      {
        value = E1 * 2^E2;
      }
      else
      {
        value = undefined;
      }
    }
    break;
  }
  </code></pre>
  
  <p>?</p>
  
  <p>Sidenote, in looking at this in terms of psudocode makes it fairly clear in my mind that @Agnew's interpretation is the correct one.</p>