Find the exponent of biggest $2^n$ power in a decimal representation

141 Views Asked by At

Given a very very long binary string, how do we find the exponent of biggest $2^n$ power in a decimal representation. We can't convert it into decimal as it will not fit any data type.

For eg - if string is $10111$ = $23_{10} $ then the answer is $16(=2^4)$

I was thinking of it has to do with the position of the leading $1$. Can someone help me out ?

1

There are 1 best solutions below

4
On BEST ANSWER

And you're thinking correct. The binary representation can be easily explained with an example. Let's take the one in question!
$\hspace{30mm}10111$ is converted to Decimal as follows -
Let $N=0$
Start from right and if you find a $1$ in the $i^{th}$ place, add $2^{i-1}$ to $N$ and increment $i$. If you find a $0$, increment $i$.

So N becomes $2^0+2^1+2^2+2^4=23$

Now, to find the highest power of 2,
Start from left, and for the first $1$ you encounter, note it's position from right. Let this be $j$. The highest power of 2 will be $j-1$.

I hope this helps.