This is my CPP solution.
class Solution {
public:
int numberOfSteps (int num) {
if(num == 0)
return num;
int cnt = 0;
while(num){
if(num%2==1)
cnt++;
cnt++;
num/=2;
}
cnt--;
return cnt;
}
};