0
$\begingroup$

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 ?

  • 0
    $10000<10111<100000$.2017-02-06

1 Answers 1

1

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.

  • 0
    With this string : $0111011011$ , the answer coming out is, j=9,so the highest power should be $2^{9-1}$ but it should be $2^{8-1}$2017-02-06
  • 0
    0111011011=475 and the nearest 2-power is 256, which is 8th power of 2. And j=9, so 9-1=8.2017-02-06
  • 0
    Sorry, I got it now !! Thanks a lot.2017-02-06
  • 0
    I'm glad I could be of help!2017-02-06