title: Why am I getting strange results bit-shifting by a negative value?
body: <p>This question is NOT a duplicate of <a href="https://stackoverflow.com/questions/1857928/right-shifting-negative-numbers-in-c">this question</a>.</p>
  
  <p>I came across a situation where I might have had to left-shift a (positive) number by a negative value, i.e., 8 &lt;&lt; -1. In that case, I would expect the result to be 4, but I'd never done this before. So I made up a little test program to verify my hypothesis:</p>
  
  <pre><code>for (int i = -8; i &lt;= 4; i++)
      Console.WriteLine("i = {0}, 8 &lt;&lt; {0} = {1}", i, 8 &lt;&lt; i);</code></pre>
  
  <p>which to my shock and surprise gave me the following output:</p>
  
  <pre>i = -8, 8 &lt;&lt; -8 = 134217728
  i = -7, 8 &lt;&lt; -7 = 268435456
  i = -6, 8 &lt;&lt; -6 = 536870912
  i = -5, 8 &lt;&lt; -5 = 1073741824
  i = -4, 8 &lt;&lt; -4 = -2147483648
  i = -3, 8 &lt;&lt; -3 = 0
  i = -2, 8 &lt;&lt; -2 = 0
  i = -1, 8 &lt;&lt; -1 = 0
  i = 0, 8 &lt;&lt; 0 = 8
  i = 1, 8 &lt;&lt; 1 = 16
  i = 2, 8 &lt;&lt; 2 = 32
  i = 3, 8 &lt;&lt; 3 = 64
  i = 4, 8 &lt;&lt; 4 = 128</pre>
  
  <p>Can anyone explain this behaviour?</p>
  
  <p>Here's a little bonus. I changed the left-shift to a right-shift, and got this output:</p>
  
  <pre>i = -8, 8 &gt;&gt; -8 = 0
  i = -7, 8 &gt;&gt; -7 = 0
  i = -6, 8 &gt;&gt; -6 = 0
  i = -5, 8 &gt;&gt; -5 = 0
  i = -4, 8 &gt;&gt; -4 = 0
  i = -3, 8 &gt;&gt; -3 = 0
  i = -2, 8 &gt;&gt; -2 = 0
  i = -1, 8 &gt;&gt; -1 = 0
  i = 0, 8 &gt;&gt; 0 = 8
  i = 1, 8 &gt;&gt; 1 = 4
  i = 2, 8 &gt;&gt; 2 = 2
  i = 3, 8 &gt;&gt; 3 = 1
  i = 4, 8 &gt;&gt; 4 = 0</pre>