JAVA | Shift operators | Log N solutions
IntuitionFind the position of leftmost set bit. Leftmost set bit will tell us how many set bits we want.ApproachTo get left most set bit maintain a counter pos
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Find the position of leftmost set bit. Leftmost set bit will tell us how many set bits we want.

# Approach
<!-- Describe your approach to solving the problem. -->
To get left most set bit maintain a counter `pos` to count the position of bits. 

Shift right by 1 `n>>1` position at a time till given becomes 0. When number becomes 0 we get the position of left most set bit.

Now shift left 1s for `1<<pos` to get power to the base 2 and substract 1 to get all set bits just greater than earlier number `1<<pos - 1`

# Complexity
- Time complexity: O(log(n))
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public int smallestNumber(int n) {
        int pos = 0;
        while(n!=0) {
            pos++;
            n = n>>1;
        }
        return (1<<pos)-1;
    }
}
```
['Math', 'Bit Manipulation', 'Java']