0 out of 464 challenges solved
Write a Python function `toggle_middle_bits(n)` that toggles all the bits of a given integer `n` except the first and the last bits. The first and last bits remain unchanged, while all other bits are flipped (0 becomes 1 and 1 becomes 0). #### Example Usage ```python [main.nopy] print(toggle_middle_bits(9)) # Output: 15 print(toggle_middle_bits(10)) # Output: 12 print(toggle_middle_bits(11)) # Output: 13 print(toggle_middle_bits(0b1000001)) # Output: 0b1111111 print(toggle_middle_bits(0b1001101)) # Output: 0b1110011 ``` #### Constraints - Input `n` is a non-negative integer. - The function should handle edge cases like `n = 0` or `n = 1` appropriately.
def toggle_middle_bits(n):
"""
Toggle all bits of the number `n` except the first and last bits.
Args:
n (int): The input number.
Returns:
int: The number with middle bits toggled.
"""
# Placeholder for the solution
pass