title: Why does left shift operation invoke Undefined Behaviour when the left side operand has negative value?
body: <p>In C bitwise left shift operation invokes Undefined Behaviour when the left side operand has negative value.</p>
  
  <p>Relevant quote from ISO C99 (6.5.7/4)</p>
  
  <blockquote>
    <p>The  result  of E1 &lt;&lt; E2 is E1 left-shifted E2 bit  positions;  vacated  bits  are  ﬁlled  with zeros.  If E1 has an unsigned type, the value of the result is E1 × 2<sup>E2</sup>, reduced modulo
    one more than the maximum value representable in the result type.  If E1 has a signed
    type and nonnegative value, and E1 × 2<sup>E2</sup> is representable in the result type, then that is
    the resulting value; otherwise, <strong>the behavior is undeﬁned</strong>.</p>
  </blockquote>
  
  <p>But in C++ the behaviour is well defined.</p>
  
  <p>ISO C++-03 (5.8/2)</p>
  
  <blockquote>
    <p>The value of E1 &lt;&lt; E2 is E1 (interpreted as a bit pattern) left-shifted E2 bit positions; vacated bits are zero-filled.  If E1 has an unsigned type, the value of the result is E1 multiplied by the quantity 2 raised to the power E2, reduced modulo ULONG_MAX+1 if E1 has type unsigned long, UINT_MAX+1 otherwise.
    [Note: the constants ULONG_MAXand UINT_MAXare defined in the header ).  ]</p>
  </blockquote>
  
  <p>That means</p>
  
  <pre><code>int a = -1, b=2, c;
  c= a &lt;&lt; b ;
  </code></pre>
  
  <p>invokes Undefined Behaviour in C but the behaviour is well defined in C++.</p>
  
  <p>What forced the ISO C++ committee to consider that behaviour well defined as opposed to the behaviour in C?</p>
  
  <p>On the other hand the behaviour is <code>implementation defined</code> for bitwise right shift operation when the left operand is negative, right?</p>
  
  <p>My question is why does left shift operation invoke Undefined Behaviour in C and why does right shift operator invoke just Implementation defined behaviour?</p>
  
  <p>P.S : Please don't give answers like "It is undefined behaviour because the Standard says so". :P</p>