Monday, December 10, 2007

log base 2

It turns out that the number 1 reason people visit this blog is to calculate log base 2 of an integer. So here is log2 of 1 through 10, to 16 digits precision:

log2(1) = 0
log2(2) = 1
log2(3) = 1.584962500721156
log2(4) = 2
log2(5) = 2.321928094887362
log2(6) = 2.584962500721156
log2(7) = 2.807354922057604
log2(8) = 3
log2(9) = 3.169925001442312
log2(10) = 3.321928094887362

Note that log2(x) is defined for any x greater than zero. If you have a calculator than computes the natural logarithm (often denoted ln), then you can calculate log2(x) = ln(x)/ln(2). The same thing works with log base 10, i.e. log2(x) = log10(x)/log10(2).

But what does it mean?
log2(x) means the power you have to raise 2 in order to get x. For example, 22 = 4, so log2(4) is 2. Similarly, 23 = 8, so log2(8) = 3. It turns out that 21.58496 is very nearly 3, so log2(3) is roughly 1.58496.

Some cases deserve special mention. log2(2) = 1 because 21 is 2. log2(1) = 0 because by mathematical convention 20 = 1 (this holds not just for 2, but for any base). Finally, note that log2(0) is undefined, although some software will return -Infinity (which is the limit of log2(x) as x approaches zero).

What is it used for?

The logarithm is useful for a variety of purposes. One of the more common is when describing exponential growth or decay. For example, the time for a radioactive substance to decay to half its mass is called the half life. Similarly we can describe accelerating growth in terms of the doubling time. I previously applied this to the number of blogs tracked by Technorati.

In computing, log2 is often used. One reason is that the number of bits needed to represent an integer n is given by rounding down log2(n) and then adding 1. For example log2(100) is about 6.643856. Rounding this down and then adding 1, we see that we need 7 bits to represent 100. Similarly, in order to have 100 leaves, a binary tree needs log2(100) levels. In the game where you have to guess a number between 1 and 100 based on whether it's higher or lower than your current guess, the average number of guesses required is log2(100) if you use a halving strategy to bracket the answer.

Two much of nothing

Although I can't provide additional help to people with logarrhythmias, I hope this note is of some assistance.

Labels: , , , , , ,

Bookmark and Share


Blogger Zeno said...

Another good use of log 2 is related to those "intelligent design" advocates who like to prattle about "complex specified information" and "information theory". When an IDiot is in full spate about how information theory disproves evolution and provides evidence for an intelligent designer, sweetly ask him (it's usually a him), "So what's log 2 all about in that context?"

Then continue to smile during the subsequent arm-waving and ducking and weaving. I hypothesize that 19 times out of 20, the IDiot will be at a loss. (Now for some field work!)

6:59 PM, December 10, 2007  
Blogger Nick Barrowman said...

Yep. I think it's a case of seeing the splinter in someone else's eye, but not the log in one's own.

7:30 PM, December 10, 2007  
Blogger yeye said...

I am stuck with a log 2 equation, so any help is appreciated :

How to get x value for :

x - 8 lg(x) = 0


6:10 PM, December 29, 2007  
Blogger Nick Barrowman said...

Well, by trial and error it's about 1.1, but to solve it properly you need to use an iterative algorithm like Newton's method. You can do it in Excel using Solver.

12:08 PM, December 30, 2007  
Blogger Piotr said...

yeye, be careful: there are two solutions, one ca. 1.12, and one ca. 35.

Once you know the ``ballpark figure'', a small 2 line program can do the rest.

8:19 PM, March 29, 2008  
Anonymous John said...

Interesting problem using natural logs. The equation ax = ln x has two solutions if a is sufficiently small, zero if too large, and one solution at the boundary. What is that boundary?

At the boundary, the slopes of both sides of the equation have to be equal -- otherwise, ln x would cross ax up and then cross back down. So at the boundary, a = 1/x.

That means, ln x = ax = a/a = 1, or x = e, and a=1/e.

10:08 PM, March 29, 2008  
Blogger Nick Barrowman said...

Thanks Piotr and John!

12:10 AM, March 30, 2008  
Anonymous Anonymous said...

I would like to thank you, I'm having digital communication and I was very confused to understand log2 …

Best regards,

1:48 PM, January 22, 2009  
Blogger Nick Barrowman said...

You are very welcome.



2:52 PM, January 22, 2009  
Anonymous Pat said...

Hi Nick,
I'm still kind of confused. I am a biochemist who is useless at maths. I just got affymatrix data back 'normalized for gene expression log2'. Can I directly compare 2 different genes? For example If I have gene 1 log2 1.2 and gene 2 log2 2.4 does that mean gene 2 is expressed twice as much as gene 1?
Kind regards,
Pat (Ireland)

3:00 PM, January 26, 2009  
Blogger Nick Barrowman said...

I don't have much experience with microarray data, but I did find a good powerpoint presentation about how such data are normalized and transformed.

But taking log2 of 2x gives 1+log2(x). So in your example, I would conclude that gene 2 is expressed a bit more than twice as much as gene 1.

By the way, it's -20 Celsius here. I wish I were in Ireland!

6:32 PM, January 26, 2009  
Anonymous Anonymous said...

Thanks very helpful, I been all over the internet and found nothing straight forward as your explanation for log2.

8:37 AM, March 30, 2009  
Blogger Nick Barrowman said...

Good to hear.

7:31 AM, March 31, 2009  
Anonymous Krishantha said...

helpful.... whats mean of ln ??????

10:50 AM, April 04, 2009  
Blogger Nick Barrowman said...

ln means the natural logarithm, which is the logarithm to the base e, where e = 2.718282...

5:56 PM, April 04, 2009  
Anonymous Anonymous said...

2 ^ 1.58496

2 ^. 3

7:16 PM, April 11, 2009  
Blogger Nick Barrowman said...

Do you have a question? (Also, note that 2^.3 is approximately equal to 1.231144.)

11:44 PM, April 11, 2009  
Anonymous Anonymous said...

Dear Barrowman,
Why you cannot calculate log2(minus data)?
Exp: Log2(-2)=?

3:04 AM, May 10, 2009  
Blogger Nick Barrowman said...

Because there's no answer. The logarithm (using any base) is only defined for positive numbers. In your example, there is no power that 2 can be raised to that will give -2.

8:52 AM, May 10, 2009  
Anonymous ThiyagarajanR said...

Thanks for the log calc.
Works great.

12:34 PM, August 07, 2009  
Anonymous Anonymous said...

Brilliantly clear explanation. This falls into the category of "stuff I used to know, should still know, but have completely forgotten!". Thanks for the old gray cell refresh!

7:25 PM, August 11, 2009  
Anonymous Anonymous said...

whats log4(2)

12:50 AM, October 11, 2009  
Blogger Nick Barrowman said...

Log base 4 of 2 is the power to which you have to raise 4 to get 2. That's 0.5 (the square root).

8:44 AM, October 11, 2009  
Anonymous Anonymous said...

can you prove that there are no integers (p/q) solutions for log base 2 of 3??

2:27 PM, October 18, 2009  
Blogger Nick Barrowman said...

I would try a proof by contradiction.

3:02 PM, October 18, 2009  
Anonymous Anonymous said...

thank you buddy........i was banging my head behind this for last 2 days!!!

12:06 AM, November 04, 2009  
Anonymous Anonymous said...

A very usefull formula! *****

1:55 PM, November 11, 2009  
Anonymous Anonymous said...


Tnx for a short and simple definition of logn(x) in log2(x) = log10(x)/log10(2)... helped a lot!


5:51 PM, November 12, 2009  
Anonymous Anonymous said...

if n ^ x = y (meaning n to the power of x = y )

then x * log(n) = log(y)
so x = log(y) / (log(n)

e.g. 2^x = 8

10:52 AM, December 10, 2009  
Blogger Nick Barrowman said...


1:21 PM, December 10, 2009  
Anonymous Anonymous said...

In reply to the question above about log2(-2):

ln(-2) = ln(2) + pi*i, where i=sqrt(-1)

so log2(-2) = ln(-2)/ln(2)

12:25 AM, January 15, 2010  
Anonymous Anonymous said...

yooooo, I just want to say thanks for the simple log base 2 calculator.

7:40 PM, January 19, 2010  
Blogger Nick Barrowman said...

No problem, glad it's helpful!

9:36 PM, January 19, 2010  
Anonymous Basu A R said...

no wonder google landed me up here... thanks a lot for the explanation..


10:48 PM, March 16, 2010  
Blogger John Paul said...

Hey, I'm doing masters n I've a problem in one of my optical fiber problems, lik
1*10^-4 = (1/N)*(.95)^(log (base 2)N) So, How do i find N now? But, the answer for N is 5301.

10:52 PM, March 17, 2010  
Anonymous Anonymous said...

can anyone say where log base 2 is exactly used?????

12:56 PM, March 29, 2010  
Anonymous rezkizuka said...

thank you
@anonymous : maybe for the bits? binary thing, binary is base of 2.

1:07 AM, April 13, 2010  
Anonymous Anonymous said...

Can you show that if N = q^k (with N, q, and k all positive integers), then either k <= logN or N = 1.

4:17 AM, April 15, 2010  
Anonymous Anonymous said...

Explanation is very apt. I am not a math student but needed to use log table for academic purpose. I went through numerous sites, but none of them explained so well as your site did. Thank you

1:49 AM, May 17, 2010  
Anonymous Ayesha said...


12:13 AM, May 25, 2010  
Anonymous Anonymous said...

how do you calculate log base 2 in Excel or Systat 12? Thanks

9:53 PM, May 30, 2010  
Blogger Nick Barrowman said...

Not sure about Systat 12, but in Excel, if you want to calculate log base 2 of 32 (for example), you type the following into a cell:


and the answer is 5, because 2 to the power of 5 is 32.

10:33 PM, May 30, 2010  
Anonymous Anonymous said...

For the person that asked where LOG is useful. A simple function that returns the binary representation of a number passed to it.

FUNCTION Bin$ (n&)
FOR p% = 0 TO INT(LOG(n& + .1) / LOG(2))
IF n& AND 2 ^ p% THEN s$ = "1" + s$ ELSE s$ = "0" + s$
IF s$ = "" THEN s$ = "0"
Bin$ = s$

8:55 PM, July 11, 2010  
Blogger Nick Barrowman said...

Thanks, Anonymous! That's terrific.

For those who don't recognize it, the function above can be used in Excel. From within Excel, you have to go into the Visual Basic Editor, and then paste that code in. For step-by-step details on that, see this article on How do I create a user-defined function in Microsoft Excel.

Once I did this, I was able to use the Bin function just as Anonymous described.

12:01 AM, July 12, 2010  
Blogger Parthiban said...

what is log -9 base 3? i think -2 is the answer. pls anyone describe me.........

3:09 PM, July 14, 2010  
Blogger Nick Barrowman said...

The logarithm of a number, regardless of the base, is only defined if that number is positive. So the log -9 base 3 is not defined.

12:11 AM, July 15, 2010  
Anonymous Ionut said...

Thanks for all the help provided, it was very useful !

12:47 PM, August 01, 2010  
Blogger GILL said...

it was really helpful... really i finally got what log base x(x)is!!!!!!
thank u

6:04 AM, September 12, 2010  
Blogger Deborah said...

log de 2 en base 7 = a
log de 28 en base 1/2 = ?


7:36 PM, October 11, 2010  
Blogger hassanazer said...

I want the rawl to change base 2 to base 10 and vise versa.

1:30 PM, October 18, 2010  
Blogger hassanazer said...

If you pls prove the equation that change base 2 to base 10 and vise versa.

1:49 PM, October 18, 2010  
Anonymous Anonymous said...

What is -(1/2)log A

1:18 PM, November 19, 2010  
Anonymous Anonymous said...

Hi .
How I CAN do THIS??
: Log 27 in base 0,2

Plase , i just need help.Thank you =D

8:32 PM, December 06, 2010  
Anonymous Anonymous said...

Simplest ever description... so fundamental and easy to digest...
Thanks you for solving my log base 2 mystery :)

11:51 PM, December 27, 2010  
Blogger sazhel said...

Hi Nick,
I came across your website while trying to understand the change-of-base-theorem to find the logarithm, but now I'm completely lost trying to solve for the exponential equation below:
5^x+2 = 3^2x-2

I was wondering if you happen to have any suggestions?

Thanks in advance

1:39 AM, March 24, 2011  
Blogger Nick Barrowman said...

I would expect that there is no closed-form solution to that equation. You could solve it numerically using an equation solver (e.g. Newton's method or a more sophisticated numerical method).

9:37 AM, March 24, 2011  
Anonymous Qesma Mohammed said...

How do you enter this in to a calculator? e.g. how would I know what power to raise the 4 to in the equation: (log 8) to the base 4. Is there an easy way to put it in to the calculator so it will tell me what power I should raise it to?
Help would be much appreciated, Thanks in advance

6:21 PM, April 27, 2011  
Blogger Nick Barrowman said...

If you would like to calculate log base 4 of 8, you would first calculate log10 of 8 (which is approximately 0.90309) and then divide that by log10 of 4 (which is approximately 0.60206), which gives 1.5. So 4 raised to the power of 1.5 is 8.

7:29 PM, April 27, 2011  
Anonymous Anonymous said...


4:34 PM, June 07, 2011  
Anonymous Anonymous said...

Natural Log:
log base e (x) < same named ln (x)

Most Known Others:
log base 2 (x)
log base 10 (x) < same named lg (x)

As well its so:
log base 2 (4)
OR you can say:
In root, you can type so:

That means log and Root are the <> of ^.

Easy way, and how to use log, i mean how to find out the x? Easy here:
log base 2 (4) = x
2^1 = 2, 2^2 = 4
x = 2
Same here:
log base 7 (343) = x
7^1 = 7, 7^2 = 49, 7^3 = 343 ;
x = 3

So you can use,know, and think what root and log is.


11:22 AM, July 10, 2011  
Anonymous Anonymous said...

Hi Nick,

Thanks for the wonderfull explanation on the log2(x) issue. I was also looking for it because of microarray data, just like someone else in this thread. You were right with your answer to him: the value represents how much gene expression in one group differs from gene expression in another group. However, once you have calculated these Logs's, they are only usefull to easily see what genes have the biggest differences between groups: being either the biggest negative number (highest expression in group 1) or the biggest positive number (being highest in group 2). The values cannot be compared with eachother as you suggested in your reply.

I have a question for myself as well: in your explantion you used the ln, but when someone asked how to put it in Excell you simply took the 10log(x)/10log(2) in stead of ln(x)/ln(2). In understand it is the same, but why then use the ln in your explantion? Are there circumstances in which it matters which one you use?

9:26 AM, July 20, 2011  
Blogger Nick Barrowman said...

Thanks, Anonymous. The only difference between the log and ln functions is a multiplicative constant, so when you take a ratio (log/log or ln/ln) it makes no difference.

To complicate matters, not everyone uses the same notation: depending on the application, log can mean log base 10, or ln, or log base 2. In most programming languages the log function computes ln, and there is often a log10 function to compute log base 10. But in Excel, the log function computes log base 10, and the ln function computes the natural logarithm.

8:48 AM, July 22, 2011  
Anonymous Angelica said...

Hi Nick, thank you so much for all information about log base 2. I was looking for it because i'm working on microarrays data too. Your explanation was very usefull for me!!

7:20 PM, July 25, 2011  
Blogger hackAlyst said...

I ended up downloading my calculator's manual, still no help.
>>log2(x) = ln(x)/ln(2). The same thing works with log base 10, i.e. >>log2(x) = log10(x)/log10(2).

These two lines were the one I was looking for! Thank you!

7:21 PM, August 19, 2011  
Anonymous Aadil Rehman Bhat said...

Thanx nick!
I had a problem related to Shannon's law, ur blog helped me to some extent.
Regards from
Aadil Rehman Bhat

6:17 AM, September 09, 2011  
Anonymous Anonymous said...

Thank you so much. You have explained it in very simple terms. It helped me a lot.

1:17 AM, November 06, 2011  
Blogger vishnu karippoor said...

thank u vry much

2:44 AM, November 09, 2011  
Anonymous Anonymous said...

Log2 is commonly used in microarray data, and many other types of biological or biochemical data in which values can increase or decrease from a baseline. The reason log2 is useful in these cases is because a 2-fold increase in gene expression (using microarray as example) would take a value from baseline of say 100% (normalized baseline) to 200%. The same magnitude decrease (2-fold) drops the value from 100% to 50%. When plotted as bar graph, it appears that the increase is greater than the decrease, but they are in fact the same magnitude. The effects get even more visually challenging when larger fold differences are plotted, say 10 or 100 fold. When converted to log2 scale, the increases and decreases of the same magnitude show the same size differences in the bar chart - a visually more pleasing and "accurate" presentation. Log2 is therefore is one way to "stop lying with statistics."

8:57 PM, November 15, 2011  
Anonymous Anonymous said...

how to solve -9/4 log base 2 of 9/4

8:00 AM, November 30, 2011  
Anonymous Yazhini said...

log base 2 of 9/4=(log base 10 9/4) * 3.32

9:29 AM, January 25, 2012  
Anonymous Curious Engineer said...

Great Post!

8:43 PM, May 29, 2012  
Anonymous Anonymous said...

wait a minute.... isnt log2(100)=10 sincei 10^2=100? why the writers claims it to be 6.643856 since 6.643856^2~= 44

I got confused....

4:54 AM, June 20, 2012  
Anonymous rehan said...

excellent work,got a lot of help :) Thank you

1:39 PM, July 13, 2012  
Anonymous Anonymous said...


5:29 AM, September 19, 2012  
Blogger Omar Gutierrez said...

Hi Nick,

Thanks for all the information about Log2. Currently I am using Log2 in excel but I just noticed that some values are slightly different to those obtained using your calculator instead.
e.g. Log(0.88,2)= -0.1776 (in excel) and using your calculator is: -0.1844

Is it something wrong with excel?



9:29 AM, September 21, 2012  
Blogger Nick Barrowman said...

That's very strange. On my copy of Excel, it gives -0.18442.

1:51 PM, September 21, 2012  
Anonymous Anonymous said...

10000 = 3000 log(base 2)[1+k]

solve for k..pls mention the steps

9:32 AM, December 04, 2012  
Anonymous Anonymous said...

i have a can i prove which is bigger without taking any number as a example? m^n or n^m?
any idea how can i prove with taking log into calculation?

7:23 AM, May 02, 2013  
Anonymous Anonymous said...

Thank you for your formula!!!

2:19 AM, August 19, 2014  
Anonymous Anonymous said...

I never learn abou a log and i am also very bed in maths and its formula, even log.
So anyone tell me that how to calculate the log base 2 easily or calculate without the calculator.....
Please help me friendsssssss

1:38 PM, August 21, 2014  
Anonymous Anonymous said...

Log2 is also used for the calculation of the entropy.

4:09 AM, September 19, 2014  
Anonymous mikeweg said...

Thank You for the help

12:34 PM, October 21, 2014  
Blogger Ravi Kumar said...

what will be the value of log base 2[1/p(x)]...
any answers?

8:53 AM, February 12, 2015  
Anonymous Anonymous said...


9:04 AM, May 27, 2015  
Blogger MeroNepalPyaroNepal said...


3:28 AM, November 14, 2015  
Blogger Pata Fea said...

I came to the log calculator because I wanted to know how to write the little 2.. I copy pasted it in the end!

1:42 PM, December 09, 2015  
Blogger wareesa ashraf said...

any body know the better formula compare to that formula to reduce the value min{1+ log2 frequency, frequency}

10:57 AM, December 26, 2015  
Blogger Unknown said...

Your arrogance is surpassed only by your ignorance.

12:59 PM, February 11, 2016  
Anonymous Anonymous said...

merge Newton log 8 power (-10) its value may be equal to Golden ratio in the following merger Its a Newton Log with golden ratio

sea land Shipping main merger 740
mares k Line main merger 640 Mares k Line
Swiss Bank Corporation main merger 540 Swiss Bank Corporation

1:17 AM, September 10, 2016  
Blogger Aderonmu adeleye said...


9:00 PM, September 27, 2016  
Blogger Mazhar Hussain said...

if log2n= 128 then how will you get n from it. ??

4:10 PM, November 02, 2016  
Blogger Mazhar Hussain said...

how to solve this equation (log2 n = 128 )for n ??

4:11 PM, November 02, 2016  
Blogger Mazhar Hussain said...

if log2n= 128 then how will you get n from it. ??

4:12 PM, November 02, 2016  
Blogger Unknown said...

This comment has been removed by the author.

11:05 AM, December 01, 2016  
Blogger Babita Bhandar Kayastha said...

2/5<log3base 10<1/2 prove that

11:07 AM, December 01, 2016  
Blogger Babita Bhandar Kayastha said...

mazhar ur answer will be 1*10^128/2

11:11 AM, December 01, 2016  
Anonymous Anonymous said...

Mazhar ur answer will be n= 2^127

=> log2(2n)=128
=> log2(2n)= log2(2^128)
=> 2n = 2^128
=> n= 2^128/2
=> n=2^127

4:26 AM, January 06, 2017  
Blogger Yila Yakubu said...

How do I get log 1/8 base 3

9:23 AM, January 11, 2017  
Blogger pankaj karnwal said...

how can i get log2 x+1

1:02 AM, January 18, 2017  

Post a Comment

<< Home