def problem_5_3(n):
    """ Given an integer, print the next smallest and next largest number that
    have the same number of 1 bits in their binary representation.

    Returns
        tuple, format (smallest, largest)
    """
    has_no_zero_bits = (n+1) & n == 0
    if has_no_zero_bits:
        raise Exception('There is no smaller number with the same number of bits')

    def count_set_bits(n):
        """ Returns the number of bits set to 1 in input. """
        count = 0
        for i in range(len(bin(n)) - 2):
            if (1 << i) & n != 0:
                count += 1
        return count

    num_bits = count_set_bits(n)

    # Find the next smallest number with the same number of bits.
    next_smallest = n
    while True:
        next_smallest -= 1
        if count_set_bits(next_smallest) == num_bits:
            break

    # Find the next larget number with the same number of bits.
    next_largest = n
    while True:
        next_largest += 1
        if count_set_bits(next_largest) == num_bits:
            break

    return (next_smallest, next_largest)