Given an integer n, you must transform it into 0 using the following operations any number of times:


	Change the rightmost (0th) bit in the binary representation of n.
	Change the ith bit in the binary representation of n if the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0.


Return the minimum number of operations to transform n into 0.

&nbsp;
Example 1:

Input: n = 3
Output: 2
Explanation: The binary representation of 3 is "11".
"11" -&gt; "01" with the 2nd operation since the 0th bit is 1.
"01" -&gt; "00" with the 1st operation.


Example 2:

Input: n = 6
Output: 4
Explanation: The binary representation of 6 is "110".
"110" -&gt; "010" with the 2nd operation since the 1st bit is 1 and 0th through 0th bits are 0.
"010" -&gt; "011" with the 1st operation.
"011" -&gt; "001" with the 2nd operation since the 0th bit is 1.
"001" -&gt; "000" with the 1st operation.


&nbsp;
Constraints:


	0 &lt;= n &lt;= 109


class Solution:
    def minimumOneBitOperations(self, n: int) -> int:
        if n <= 1:
            return n
        def leftmostbit(x):
            x |= x >> 1
            x |= x >> 2
            x |= x >> 4
            x |= x >> 8
            x |= x >> 16
            x += 1
            x >>= 1
            return x
        x = leftmostbit(n)
        return ((x << 1) - 1) - self.minimumOneBitOperations(n - x)
class Solution {
    public int minimumOneBitOperations(int n) {

    int inv = 0;

        // xor until n becomes zero
        for ( ; n != 0 ; n = n >> 1){

            inv ^= n;
            System.out.println(inv+" "+n);
        }

        return inv;
    }
}
class Solution {
public:
    int minimumOneBitOperations(int n) {
        int output = 0;
        
        while( n> 0)
        {
            output ^= n;
            n = n >> 1;
        }
        
        return output;
    }
};
/**
 * @param {number} n
 * @return {number}
 */
var minimumOneBitOperations = function(n) {
  let answer = 0;
  let op = 1;
  let bits = 30;
  while(bits >= 0) {
    if(n & (1 << bits)) {
      let tmp = (1 << (bits + 1)) - 1;
      answer += tmp * op;
      op *= -1;
    }
    bits--;
  }
  return answer;
}
Minimum One Bit Operations to Make Integers Zero