title: Can someone explain to me what this GetCardinality method is doing?
body: <p>I've been looking into faceted search with Lucene.NET, I've found a brilliant example <a href="http://www.devatwork.nl/index.php/articles/lucenenet/faceted-search-and-drill-down-lucenenet/" rel="noreferrer">here</a> which explains a fair amount, apart from the fact that it completely overlooks the function which checks the cardinality of items in a bit array.</p>
  
  <p>Can anyone give me a run down of what it is doing? The main things I don't understand is why the bitsSetArray is created as it is, what it is used for and how all the if statements work in the for loop.</p>
  
  <p>This may be a big ask but I have to understand how this works before I can even think of using it in my own code.</p>
  
  <p>Thanks</p>
  
  <pre><code>public static int GetCardinality(BitArray bitArray)
      {
          var _bitsSetArray256 = new byte[] {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
          var array = (uint[])bitArray.GetType().GetField("m_array", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(bitArray);
          int count = 0;
  
          for (int index = 0; index &lt; array.Length; index ++)
              count += _bitsSetArray256[array[index] &amp; 0xFF] + _bitsSetArray256[(array[index] &gt;&gt; 8) &amp; 0xFF] + _bitsSetArray256[(array[index] &gt;&gt; 16) &amp; 0xFF] + _bitsSetArray256[(array[index] &gt;&gt; 24) &amp; 0xFF];
  
          return count;
      }
  </code></pre>