Number of Steps to Reduce a Number to Zero

Easy

Given num, repeat these rules until it reaches 0 and return the number of steps used:

  • if the number is even, divide it by 2
  • if it is odd, subtract 1

Example 1

numberOfSteps()
Input
num = 14
Output
6

Why

14 → 7 → 6 → 3 → 2 → 1 → 0

Example 2

numberOfSteps()
Input
num = 8
Output
4
Constraints
0 <= num <= 10^6

Topics

MathBit Manipulation

Implement numberOfSteps in the editor and return the answer — printing it is never graded.