title: Dividing by power of 2 using bit shifting
body: <p>I've got the following task:</p>
  <blockquote>
  <p>Compute <code>x/(2^n)</code>, for <code>0 &lt;= n &lt;= 30</code> using bit shifting.</p>
  <p>Requirement: Round toward zero.</p>
  <p>Examples:</p>
  <pre><code>divpwr2(15,1) = 7
  divpwr2(-33,4) = -2
  </code></pre>
  <p>Legal operators: <code>! ~ &amp; ^ | + &lt;&lt; &gt;&gt;</code></p>
  <p>Maximum number of operators: 15</p>
  </blockquote>
  <p>Here is what I've got so far:</p>
  <pre><code>public int DivideByPowerOf2(int x, int n)
  {
      //TODO: find out why DivideByPowerOf2(-33,4) = -3 instead of -2
      return x &gt;&gt; n;
  }
  </code></pre>
  <p><code>DivideByPowerOf2(15,1) = 7</code> is ok.</p>
  <p>But <code>DivideByPowerOf2(-33,4) = -3</code> instead of -2. Why?</p>