Python Program to Clear the Rightmost Set Bit of a Number

bookmark

def clear_rightmost_set_bit(n):
    """Clear rightmost set bit of n and return it."""
    return n & (n - 1)
 
 
n = int(input('Enter a number: '))
ans = clear_rightmost_set_bit(n)
print('n with its rightmost set bit cleared equals:', ans)

 

Output


Enter a number: 5
n with its rightmost set bit cleared equals: 4