Given a C code
for(i=n,j=0 , i>0,i/=2 , j+=i)
What is the value of j after termination of for loop?
In the solution in my book, it starts with:
j=n+n/2 +n/4+....+log n terms.
Now I can understand how there are log n terms in the above series.
Any help appreciated, thanks.
It is log2 n, that is the number of binary bits (or the rank of the highest 1 bit) in the binary representation of n
And you made a typo, you probably mean
So you start with
i==nthen you halve it till 0 is reached (hence the log2 n number of loops).