title: How to bit-shift a register to zero?
body: <p>When you left-shift (&lt;&lt;) or right-shift (>>) an integer register, you pull in zeros from the opposite side. It stands to reason that if you shift by more than the width of a register (e.g. shift a uint by 32 or more bits), it should become all zeros. In C#, this is not what happens. Instead, the shift count is effectively interpreted mod the register width, so it is impossible to definitively make the register zero using shifts. This is a pain.</p>
  
  <p>I got hit by this because I am representing an extra-large integer via a unit[]. When you perform bit shifts on the extra-large register, in general bits from different uint instances in the array must "mix", so the code does stuff like:</p>
  
  <pre><code>  s[i] = (s[i+k] &lt;&lt; n) | (s[i+k+1] &gt;&gt; (32 - n))
  </code></pre>
  
  <p>but this code fails when n = 0 or 32, i.e. when the shift corresponds to a pure shift among uint values with no mixing. (It's easy to write the code so that n = 32 never appears, but then the shift by (32 - n) still fails when n = 0.) For now, I have got around this problem by special-casing n = 0, but this is extra code (ugly) and an extra test-and-branch in a performance-critical spot (bad).</p>
  
  <p>Can anyone suggest a modification (maybe using masking instead of or in addition to bit shifting?) to get around this difficulty? Can anyone explain why C# made the choice to make shift counts not behave according to naive expectations?</p>