# Bitwise Algorithm 2: Clear Lowest Bit

### Problem

Clear the rightmost set bit in the given integer.

For example, given the number , the bit pattern is . Clearing the right most set bit we get , which is . Given the number , the bit pattern is , clearing the right most set bit we get .

### Algorithm

The algorithm is simple. The difference in bit pattern between two consecutive binary numbers ( and ) is such that the rightmost set bit in the greater number is not set in the lesser number. However, all bits to the left of the rightmost set bit in the greater number are the same in both numbers. Therefore, if we bitwise AND the two consequence numbers together, we get the number in which the rightmost set bit from the greater number is unset, but all the other bits are kept the same.

Here is the code in java:

public static int clearLowestBit(int x) { return x & (x-1); }