/// <summary> /// Returns the number of bits in the binary representation of this which differ from the sign bit. /// <para>Use BitLength(0) if you want to know the length of the binary value in bits. /// If this is positive the result is equivalent to the number of bits set in the binary representation of this. /// If this is negative the result is equivalent to the number of bits set in the binary representation of -this - 1.</para> /// </summary> internal static int BitCount(BigInteger Value) { int bCount = 0; if (Value._sign == 0) { return(0); } int i = Value.FirstNonzeroDigit;; if (Value._sign > 0) { for (; i < Value._numberLength; i++) { bCount += IntUtils.BitCount(Value._digits[i]); } } else { // this digit absorbs the carry bCount += IntUtils.BitCount(-Value._digits[i]); for (i++; i < Value._numberLength; i++) { bCount += IntUtils.BitCount(~Value._digits[i]); } // We take the complement sum: bCount = (Value._numberLength << 5) - bCount; } return(bCount); }