def invert_bitstring_with_mask(string: int, masklen: int) -> int:
    """Invert a bitstring with a mask.

    Args:
        string (bitstring) - the bitstring to invert
        masklen (int) - the value to mask the inverted bitstring to

    Returns:
        (bitstring) - a bitstring inverted up to the masking length
    """
    mask = (1 << masklen) - 1
    return ~string & mask