CyrusBuilt.MonoPi.BitSet.Cardinality C# (CSharp) Method

Cardinality() public method

Returns the number of bits set to true in this bit set.
public Cardinality ( ) : Int32
return System.Int32
		public Int32 Cardinality() {
			Int32 card = 0;
			long a = 0L;
			Int32 b = 0;
			for (Int32 i = this._bits.Length - 1; i >= 0; i--) {
				a = this._bits[i];
				// Take care of common cases.
				if (a == 0) {
					continue;
				}

				if (a == -1) {
					card += 64;
					continue;
				}

				// Successively collapse alternating bit groups into a sum.
				a = ((a >> 1) & 0x5555555555555555L) + (a & 0x5555555555555555L);
				a = ((a >> 2) & 0x3333333333333333L) + (a & 0x3333333333333333L);
				b = (Int32)((a >> 32) + a);
				b = ((b >> 4) & 0x0f0f0f0f) + (b & 0x0f0f0f0f);
				b = ((b >> 8) & 0x00ff00ff) + (b & 0x00ff00ff);
				card += ((b >> 16) & 0x0000ffff) + (b & 0x0000ffff);
			}
			return card;
		}