# Find closest power of 2 to a specific number

This question is weird and it's not a homework question. I couldn't come up with anything substantial, so sorry if you think that I should have posted my tried methods (I have tried but most of them were fails).

So, suppose I have a number $$n$$. Its parity doesn't matter(whether it is even or odd is to no interest to us). Just by having $$n$$ is there a way to find $$2^m$$ where when $$2$$ raised to the power of $$m$$, it is the value that is closest to $$n$$?

Like for example, if $$n = 7$$ then the closest power of $$2$$ is $$2^3$$ which is $$8$$.

But my question is:

Is there a function with which I can calculate this?

If there was something that I failed to represent and as a result, you didn't understand, please comment. Also, I don't know the tag to put this in, somebody please help me.

• Are you familiar with logarithms? Oct 5 '19 at 15:37
• @saulspatz Not really, my knowledge doesn't extend much from algebra. Oct 5 '19 at 15:37
• @saulspatz It would be nice, if you could give me the solution to this with logarithms, or even just some idea about the right direction to go :) Oct 5 '19 at 15:42

This is not a weird question at all, but I don't think this is the best forum for a lesson on logarithms. You'll find lots of stuff on the Web if you Google "logarithm." I suggest you look at some of the lessons, and come back here if you have questions. In the meantime, I'll explain how to answer the question.

What you want is the base-$$2$$ logarithm. For example $$\log_2 7=x$$ means the same thing as $$2^x=7.$$ Now, to compute $$\log_2{7}$$ we can use WolframAlpha and find $$\log_2{7}\approx2.8$$ This tells us that the $$n$$ we seek is either $$2$$ or $$3$$. While it's easy to guess that the answer is $$3$$ in this case, until you know more about logarithms, I suggest you just compute bot $$2^2$$ and $$2^3$$ and see which is closer. It's not always true that you can just take the integer closest to the logarithm.

Hope this helps.

EDIT

Let's say that $$t=\left\lfloor\log_2 n\right\rfloor$$, that is, $$t$$ is the greatest integer less than or equal to $$\log_2 n$$, so we know $$2^t\leq n< 2^{t+1}$$

We want to set $$m=t$$ if $$n\leq \frac32\cdot2^t$$ that is, if $$\log_2 n<\log_2\frac32+t=t+.584963...$$

so you compute $$\log_2 n$$, and round down if the integer part is less than $$.584963$$, otherwise, round up.

If $$n$$ happens to be very close to $$3\cdot2^m$$ for some integer $$m$$, you may still have to compute $$2^m$$ and $$2^{m+1}$$ to be sure which to take, because of roundoff error in computing the logarithm.

• Thank you very much for your advice. Yes, I should see what logarithms are and then try to tackle this problem. Once I do I will select this as the answer. Thanks :) Oct 5 '19 at 15:55
• Thank you very much, this helped me solve my problem! Oct 6 '19 at 8:05
• Hey I know this is an old question, I mean 2020 has passed, this is way old. But can you tell me how do you pick the right value? I'm referring to the fact that I have to manually pick between 2 and 3 to see which is the right one, but is there a way to do it automatically? Like, have a function that just straight outputs it? Or it's not possible? Thanks a lot for your help :) Aug 4 '21 at 7:58
• @GauravMall See my edit. Aug 4 '21 at 14:38
• Thanks a lot for the edit. So, from what I understand, there isn't a foolproof technique, but just a really good approximation. For what I need though this is amazing. Thanks! Aug 5 '21 at 8:53