Number of steps to reduce a number to 0 by reducing "number/2" (integer Division eg. 3/2=1) in each step

594 Views Asked by At

I want to know that is it possible to tell directly the number of steps to reaching 0 from a given number given that a number can be reduce to its half in each step. For Example :

9 step 1:9/2=4 (Integer)

step 2:4/2=2

step 3:2/2=1

step 4:1/2=0

So number of steps 4.

IS there any Direct way for this ????

1

There are 1 best solutions below

0
On

Usually to know how many steps you need to reduce a number to $1$ by dividing by $2$ calculate $log_2^a$, where $a$ is your target number. As the answer should be integer take $\lfloor log_2^a \rfloor$ (round down). Finally add one as you want the number to reach $0$ not $1$.