title: c# convert a 32bit BitArray to a UInt32
body: <p>I am trying to convert 32 bits in a BitArray to a UInt32 in a 32bit c# NET4.0 app in Win7 64bit.</p>
  
  <p>I have tried two techniques but both throw an exception.</p>
  
  <p>When using CopyTo, it throws this:
  <strong>System.ArrayTypeMismatchException: Source array type cannot be assigned to destination array type.</strong></p>
  
  <pre><code>BitArray newBits = new BitArray(32);
  // (modify bits...)
  
  UInt32[] intArray = new UInt32[1];
  newBits.CopyTo(intArray, 0); // &lt; crash
  </code></pre>
  
  <p>When manually setting the bits, it throws this when it gets to the 32nd bit (when i = 31):
  <strong>System.OverflowException: Arithmetic operation resulted in an overflow.</strong> </p>
  
  <pre><code>BitArray newBits = new BitArray(32);
  // (modify bits...) 
  
  UInt32 res = 0;
  for (int i = 0 ; i &lt; newBits.Length ; i++) 
  {
      //Debug("i="+i+", "+newBits.Length);
      if (newBits[i]) res |= (UInt32)(1 &lt;&lt; i); // &lt; crash when i reaches 31
  }   
  </code></pre>
  
  <ol>
  <li>Why can't BitArray copy to an unsigned int?</li>
  <li>Why does '1 &lt;&lt; i' fail when i = 31 in a 32bit variable? </li>
  </ol>
  
  <p>(sorry for the duplicate question, I tried commenting in the other post but I need 50 reputation)
  <a href="https://stackoverflow.com/questions/37157550/converting-a-bitarray-in-to-uint32-c-sharp">Converting a BitArray in to UInt32 C#</a></p>