---
# System prepended metadata

title: 1342. Number of Steps to Reduce a Number to Zero
tags: [Leetcode, Bit Manipulation, Medium]

---

# 1342. Number of Steps to Reduce a Number to Zero
###### tags: `Leetcode` `Medium` `Bit Manipulation`
Link: https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/
## 思路
算出num的二进制表达式，遇到一个1，答案就加2，遇到0，答案加1
最后的答案要-1，因为当num变成1的时候，只需要subtract 1即可，不再需要/2
## Code
```java=
class Solution {
    public int numberOfSteps(int num) {
        if(num==0) return 0;
        int ans = 0;
        while(num!=0){
            ans += (num&1)==1?2:1;
            num >>= 1;
        }
        return ans-1;
    }
}
```