def mask_out_bits(segbits, mask, tags_to_mask=None):
    """
    Given a set of bits and a list of tags to affect (optional) removes all
    the bits from each tag that are present (and equal) in the masking set.
    """

    if tags_to_mask is None:
        tags_to_mask = segbits.keys()

    # Mask out matching bits
    for tag in tags_to_mask:
        bits = segbits[tag]

        bits = set(bits) - set(mask)
        segbits[tag] = bits

    return segbits