def split(range, upper):
    """
    >>> split((0, 127), False)
    (0, 63)
    >>> split((0, 63), True)
    (32, 63)
    >>> split((32, 63), False)
    (32, 47)
    >>> split((32, 47), True)
    (40, 47)
    >>> split((40, 47), True)
    (44, 47)
    >>> split((44, 47), False)
    (44, 45)
    >>> split((44, 45), False)
    (44, 44)
    >>> split((0, 7), True)
    (4, 7)
    >>> split((4, 7), False)
    (4, 5)
    >>> split((4, 5), True)
    (5, 5)
    """

    start, end = range
    length = (end - start + 1) // 2

    if upper:
        return (start + length, end)
    else:
        return (start, end - length)