Determine the number of bits to pack/unpack.
977324
def _get_bits(bits=None, target=None):
    

    if bits is not None:
        bits = int(bits)
        if bits in (8, 16, 32, 64):
            return bits
        else:
            raise ValueError('bits needs to be 8, 16, 32 or 64')
    else:
        return int((target if target is not None else pwnypack.target.target).bits)
510181
def bits(self, count):
        

        if count < 0:
            raise ValueError

        if count > self._bits:
            n_bytes = (count - self._bits + 7) // 8
            data = self._fileobj.read(n_bytes)
            if len(data) != n_bytes:
                raise BitReaderError("not enough data")
            for b in bytearray(data):
                self._buffer = (self._buffer << 8) | b
            self._bits += n_bytes * 8

        self._bits -= count
        value = self._buffer >> self._bits
        self._buffer &= (1 << self._bits) - 1
        assert self._bits < 8
        return value
1126156
def random_bits(bits_count):
	
	bytes_count = int(math.ceil(bits_count / 8))
	random_value = int.from_bytes(os.urandom(bytes_count), byteorder=sys.byteorder)
	result_bits = bytes_count * 8

	if result_bits > bits_count:
		random_value = (random_value >> (result_bits - bits_count))

	return random_value
316139
def pack(chunks, r=32):
    
    if r < 1:
        raise ValueError('pack needs r > 0')

    n = shift = 0

    for c in chunks:
        n += c << shift
        shift += r

    return n
565622
def pack_data(self, remaining_size):
        """"""
        payload = self.part_struct.pack(self.locator_id, self.readoffset + 1, self.readlength, b'    ')
        return 4, payload
956106
private void doWrite(int bits, int count) {

		int frontBits = ((int) position) & 31;
		int firstInt = (int) (position >> 5);

		int sumBits = count + frontBits;
		if (sumBits <= 32) {
			int i = ints[firstInt];
			int mask = frontMask(frontBits) | backMask(sumBits);
			i &= mask;
			i |= (bits << (32 - sumBits)) & ~mask;
			ints[firstInt] = i;
		} else {
			int i = ints[firstInt];
			int mask = frontMask(frontBits);
			i &= mask;
			int lostBits = sumBits - 32;
			i |= (bits >> lostBits) & ~mask;
			ints[firstInt] = i;

			i = ints[firstInt + 1];
			mask = backMask(lostBits);
			i &= mask;
			i |= (bits << (32 - lostBits)) & ~mask;
			ints[firstInt + 1] = i;
		}
	}
71135
public int count()
    {
        int count = 0;
        for (int ii=bits-1;ii>=0;ii--)
        {
            if (isSet(ii))
            {
                count++;
            }
        }
        return count;
    }
316317
int unpack_info(Buffer opb) {
        version = opb.read(32);
        if (version != 0) {
            return (-1);
        }

        channels = opb.read(8);
        rate = opb.read(32);

        bitrate_upper = opb.read(32);
        bitrate_nominal = opb.read(32);
        bitrate_lower = opb.read(32);

        blocksizes[0] = 1 << opb.read(4);
        blocksizes[1] = 1 << opb.read(4);

        if ((rate < 1) || (channels < 1) || (blocksizes[0] < 8) || (blocksizes[1] < blocksizes[0])
                || (opb.read(1) != 1)) {
            clear();
            return (-1);
        }
        return (0);
    }
720988
def binned_bitsets_from_list( list=[] ):
    """"""
    last_chrom = None
    last_bitset = None
    bitsets = dict()
    for l in list:
        chrom = l[0]
        if chrom != last_chrom:
            if chrom not in bitsets:
                bitsets[chrom] = BinnedBitSet(MAX)
            last_chrom = chrom
            last_bitset = bitsets[chrom]
        start, end = int( l[1] ), int( l[2] )
        last_bitset.set_range( start, end - start )
    return bitsets
1257903
private int nextSetBit(int fromIndex) {
      if (fromIndex < 0)
         throw new IndexOutOfBoundsException("fromIndex < 0: " + fromIndex);

      int u = intIndex(fromIndex);
      int arrayLength = array.length();
      if (u >= arrayLength)
         return -1;

      int possible = array.get(u) & (INT_MASK << fromIndex);

      while (true) {
         if (possible != 0) {
            return (u << ADDRESS_BITS_PER_INT) + Integer.numberOfTrailingZeros(possible);
         }
         if (++u == arrayLength) {
            return -1;
         }
         possible = array.get(u);
      }
   }
973239
def size(self) -> Optional[int]:
        """"""
        if not self._parts:
            return 0

        total = 0
        for part, encoding, te_encoding in self._parts:
            if encoding or te_encoding or part.size is None:
                return None

            total += int(
                2 + len(self._boundary) + 2 +  # b'--'+self._boundary+b'\r\n'
                part.size + len(part._binary_headers) +
                2  # b'\r\n'
            )

        total += 2 + len(self._boundary) + 4  # b'--'+self._boundary+b'--\r\n'
        return total
425764
def _determine_nTrackIterations(self,nTrackIterations):
        """"""
        if not nTrackIterations is None:
            self.nTrackIterations= nTrackIterations
            return None
        if numpy.fabs(self.misalignment(quantity=False)) < 1./180.*numpy.pi:
            self.nTrackIterations= 0
        elif numpy.fabs(self.misalignment(quantity=False)) >= 1./180.*numpy.pi \
                and numpy.fabs(self.misalignment(quantity=False)) < 3./180.*numpy.pi:
            self.nTrackIterations= 1
        elif numpy.fabs(self.misalignment(quantity=False)) >= 3./180.*numpy.pi:
            self.nTrackIterations= 2
        return None
417087
private int countNumberOfBitsSetToOne() {

    AtomicInteger numberOfBitsSetToOne = new AtomicInteger(0);

    stream(getBitArray()).forEach(bucket -> {
      for (int bitMask : BIT_MASKS) {
        if ((bucket & bitMask) != 0) {
          numberOfBitsSetToOne.incrementAndGet();
        }
      }
    });

    return numberOfBitsSetToOne.get();
  }
337387
def toBCD (n):

    """"""
    bcd  = 0
    bits = 0

    while True:
        n, r  = divmod(n, 10)
        bcd  |= (r << bits)
        if n is 0:
            break
        bits += 4

    return bcd
184771
def read_bits(self, num):
        
        if num > len(self._bits):
            needed = num - len(self._bits)
            num_bytes = int(math.ceil(needed / 8.0))
            read_bytes = self._stream.read(num_bytes)

            for bit in bytes_to_bits(read_bytes):
                self._bits.append(bit)

        res = []
        while len(res) < num and len(self._bits) > 0:
            res.append(self._bits.popleft())

        return res
627689
def target_from_compact(bits):
    """"""
    size = bits >> 24
    word = bits & 0x007fffff
    if size < 3:
        word >>= 8 * (3 - size)
    else:
        word <<= 8 * (size - 3)
    if bits & 0x00800000:
        word = -word
    return word
436867
def _number_of_set_bits(x):
    '''
    
    '''
    # Taken from http://stackoverflow.com/a/4912729. Many thanks!
    x -= (x >> 1) & 0x55555555
    x = ((x >> 2) & 0x33333333) + (x & 0x33333333)
    x = ((x >> 4) + x) & 0x0f0f0f0f
    x += x >> 8
    x += x >> 16
    return x & 0x0000003f