// CountBitsUint8s count 1's in b
195432
func CountBitsUint8s(b []uint8) int {
	c := 0
	for _, x := range b {
		c += CountBitsUint8(x)
	}
	return c
}
267742
func Uint32s(b0, b1 []uint32) int {
	d := 0
	for i, x := range b0 {
		d += Uint32(x, b1[i])
	}
	return d
}
1308698
func CountBitsByteAlt(x byte) int {
	x = (x & 0x55) + ((x >> 1) & 0x55)
	x = (x & 0x33) + ((x >> 2) & 0x33)
	return int((x & 0x0f) + ((x >> 4) & 0x0f))
}
626363
func (r *Redis) BitCount(key string, start, end int) (int64, error) {
	rp, err := r.ExecuteCommand("BITCOUNT", key, start, end)
	if err != nil {
		return 0, err
	}
	return rp.IntegerValue()
}
207255
func countPsubs(subs []*Subscriber) int {
	n := 0
	for _, p := range subs {
		n += len(p.patterns)
	}
	return n
}
714266
func BitCountUint(x uint) int {
	var n = uint64(x)
	n -= (n >> 1) & 0x5555555555555555
	n = (n>>2)&0x3333333333333333 + n&0x3333333333333333
	n += n >> 4
	n &= 0x0f0f0f0f0f0f0f0f
	n *= 0x0101010101010101

	return int(n >> 56)
}
396222
func countBits(n *big.Int) int {
	var count int = 0
	for _, b := range n.Bytes() {
		count += int(bitCounts[b])
	}
	return count
}
586991
func BitCount(x int32) int {
	x = x - ((x >> 1) & 0x55555555)
	x = (((x >> 2) & 0x33333333) + (x & 0x33333333))
	x = (((x >> 4) + x) & 0x0f0f0f0f)
	return int(((x * 0x01010101) >> 24))
}
657943
func CountBitsUints(b []uint) int {
	c := 0
	for _, x := range b {
		c += CountBitsUint(x)
	}
	return c
}
1123194
func CountBitmap(bitmap, sbit C.MMBitmapRef, args ...float32) int {
	var tolerance C.float = 0.01
	if len(args) > 0 {
		tolerance = C.float(args[0])
	}

	count := C.count_of_bitmap(bitmap, sbit, tolerance)
	return int(count)
}
628952
func (b *BitReader) ReadBit() (Bit, error) {
	if b.count == 0 {
		if n, err := b.r.Read(b.b[:]); n != 1 || (err != nil && err != io.EOF) {
			return Zero, err
		}
		b.count = 8
	}
	b.count--
	d := (b.b[0] & 0x80)
	b.b[0] <<= 1
	return d != 0, nil
}
745062
func ConvertBits(data []byte, fromBits, toBits uint8, pad bool) ([]byte, error) {
	if fromBits < 1 || fromBits > 8 || toBits < 1 || toBits > 8 {
		return nil, fmt.Errorf("only bit groups between 1 and 8 allowed")
	}

	// The final bytes, each byte encoding toBits bits.
	var regrouped []byte

	// Keep track of the next byte we create and how many bits we have
	// added to it out of the toBits goal.
	nextByte := byte(0)
	filledBits := uint8(0)

	for _, b := range data {

		// Discard unused bits.
		b = b << (8 - fromBits)

		// How many bits remaining to extract from the input data.
		remFromBits := fromBits
		for remFromBits > 0 {
			// How many bits remaining to be added to the next byte.
			remToBits := toBits - filledBits

			// The number of bytes to next extract is the minimum of
			// remFromBits and remToBits.
			toExtract := remFromBits
			if remToBits < toExtract {
				toExtract = remToBits
			}

			// Add the next bits to nextByte, shifting the already
			// added bits to the left.
			nextByte = (nextByte << toExtract) | (b >> (8 - toExtract))

			// Discard the bits we just extracted and get ready for
			// next iteration.
			b = b << toExtract
			remFromBits -= toExtract
			filledBits += toExtract

			// If the nextByte is completely filled, we add it to
			// our regrouped bytes and start on the next byte.
			if filledBits == toBits {
				regrouped = append(regrouped, nextByte)
				filledBits = 0
				nextByte = 0
			}
		}
	}

	// We pad any unfinished group if specified.
	if pad && filledBits > 0 {
		nextByte = nextByte << (toBits - filledBits)
		regrouped = append(regrouped, nextByte)
		filledBits = 0
		nextByte = 0
	}

	// Any incomplete group must be <= 4 bits, and all zeroes.
	if filledBits > 0 && (filledBits > 4 || nextByte != 0) {
		return nil, fmt.Errorf("invalid incomplete group")
	}

	return regrouped, nil
}
306729
func toBits(s byte) [8]bool {
	r := [8]bool{}
	for i := range r {
		if s&(uint8(1)<<uint8(7-i)) != 0 {
			r[i] = true
		}
	}
	return r
}
396895
func CountBitsUint64(x uint64) int {
	// put count of each 2 bits into those 2 bits
	x -= (x >> 1) & m1q

	// put count of each 4 bits into those 4 bits
	x = (x & m2q) + ((x >> 2) & m2q)

	// put count of each 8 bits into those 8 bits
	x = (x + (x >> 4)) & m4q

	// returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ...
	return int((x * hq) >> 56)
}
241360
func (s *Sublist) Count() uint32 {
	s.RLock()
	defer s.RUnlock()
	return s.count
}
694671
func SeqCount(s0 iseq.Seq) int {
	i := 1 // if we are here, it is non-empty
	for s := s0.Next(); s != nil; s, i = s.Next(), i+1 {
		if cnt, ok := s.(iseq.Counted); ok {
			return i + cnt.Count1()
		}
	}
	return i
}
392076
func countTrailingZeros(x uint64) int {
	// x & -x leaves only the right-most bit set in the word. Let k be the
	// index of that bit. Since only a single bit is set, the value is two
	// to the power of k. Multiplying by a power of two is equivalent to
	// left shifting, in this case by k bits. The de Bruijn constant is
	// such that all six bit, consecutive substrings are distinct.
	// Therefore, if we have a left shifted version of this constant we can
	// find by how many bits it was shifted by looking at which six bit
	// substring ended up at the top of the word.
	// (Knuth, volume 4, section 7.3.1)
	if x == 0 {
		// We have to special case 0; the fomula
		// below doesn't work for 0.
		return 64
	}
	return int(deBruijn64Lookup[((x&-x)*(deBruijn64))>>58])
}