def throw_random_bits(lengths, mask, allow_overlap=False): rval = BitSet(mask.size) throw_random_gap_list(lengths, mask, lambda s, e: rval.set_range(s, e - s), allow_overlap) if not allow_overlap: assert rval.count_range(0, rval.size) == sum(lengths) return rval
def count_overlap(bits1, bits2): """ Count the number of bits that overlap between two sets """ b = BitSet(bits1.size) b |= bits1 b &= bits2 return b.count_range(0, b.size)