title: Bit Array Equality
body: <p>I need something a little more than the <code>System.Collections.BitArray</code> class in my application. Specifically, I need the bit array:</p>
  
  <ul>
  <li>To be immutable</li>
  <li>To implement equality using value semantics</li>
  </ul>
  
  <p>I created my own <code>struct</code>, largely copying the internals of the <code>BitArray</code> implementation. (Thanks, <a href="http://www.red-gate.com/products/reflector/" rel="noreferrer">.Net Reflector</a>!)</p>
  
  <p>I don't deal everyday with bitwise operations, so I don't have the highest degree of confidence in my equality implementation. (It's passing the unit tests I am throwing at it, but I may be missing edge cases.) I have my proposed solutions as answers below. I would appreciate others' feedback and answers for something that may be more correct or efficient.</p>
  
  <p>Just like the CLR <code>BitArray</code>, the <code>length</code> field refers to the number of bits in the struct and the <code>array</code> field (or <code>Array</code> property) refers to the 32-bit integer array that represents the bits.</p>
  
  <p><strong>[CLARIFICATION]</strong> I have chosen to take the easy route in my constructors and other methods so that I cannot rely on the unnecessary bits being zeros. For example,</p>
  
  <ul>
  <li><code>Not()</code> is implemented by bitwise negation (<code>~</code>) on the integer array elements.</li>
  <li>A constructor is available that takes a length and boolean to initialize all bits. If the initialization value is true, I set all elements of the int array to -1 (in two's complement, represented by all 1's)</li>
  <li>Etc.</li>
  </ul>
  
  <p>Thus, I need to handle (or, rather, ignore) them in the comparison. A fine solution would also be to keep those bits zeroed at all times, but in my situation that will result in more work (both for the computer and me!)</p>