def set_bit(bitmask: int, bit: int, flag: bool = True) -> int:
    """
    Set a bit in a bitmask.
    :param bitmask:     The initial bitmask.
    :param bit:         The bit to be set.
    :param flag:        True to set the bit high, False to set the bit low.
    :returns:           The updated bitmask.
    """
    if flag:
        bitmask |= (1 << bit)
    else:
        bitmask &= ~(1 << bit)
    return bitmask