Counting bits set in a .Net BitArray Class
This is my solution based on the “best bit counting method” from http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel public static Int32 GetCardinality(BitArray bitArray) { Int32[] ints = new Int32[(bitArray.Count >> 5) + 1]; bitArray.CopyTo(ints, 0); Int32 count = 0; // fix for not truncated bits in last integer that may have been set to true with SetAll() ints[ints.Length – 1] … Read more