World Library  
Flag as Inappropriate
Email this Article

Binary logarithm

Article Id: WHEBN0000249992
Reproduction Date:

Title: Binary logarithm  
Author: World Heritage Encyclopedia
Language: English
Subject: Logarithm, Binary search algorithm, Password strength, Logarithms, Nat (unit)
Collection: Binary Arithmetic, Calculus, Logarithms
Publisher: World Heritage Encyclopedia

Binary logarithm

Plot of log2 n

In mathematics, the binary logarithm (log2 n) is the logarithm to the base 2. It is the inverse function of the power of two function. The binary logarithm of a positive number n is the power to which the number 2 must be raised to obtain the value n. That is:

x=\log_2 n \quad\Longleftrightarrow\quad 2^x=n.

For example, the binary logarithm of 1 is 0, the binary logarithm of 2 is 1, the binary logarithm of 4 is 2, the binary logarithm of 8 is 3, the binary logarithm of 16 is 4 and the binary logarithm of 32 is 5.

The binary logarithm is closely connected to the binary numeral system. Historically, the first application of binary logarithms was in music theory, by Leonhard Euler. Other areas in which the binary logarithm is frequently used include information theory, combinatorics, computer science, bioinformatics, the design of sports tournaments, and photography.


  • History 1
  • Notation 2
  • Applications 3
    • Information theory 3.1
    • Combinatorics 3.2
    • Computational complexity 3.3
    • Bioinformatics 3.4
    • Music theory 3.5
    • Sports scheduling 3.6
    • Photography 3.7
  • Calculation 4
    • Conversion from other bases 4.1
    • Integer rounding 4.2
    • Recursive approximation 4.3
    • Software library support 4.4
  • References 5
  • External links 6


Leonhard Euler was the first to apply binary logarithms to music theory, in 1739.

Michael Stifel has been credited with publishing the first known table of binary logarithms, in 1544. His book Arthmetica Integra contains several tables that show the integers with their corresponding powers of two. Reversing the rows of these tables allow them to be interpreted as tables of binary logarithms.[1][2]

Binary logarithms were considered more explicitly by Leonhard Euler in 1739. Euler established their application to music theory, long before their more significant applications in information theory and computer science became known. As part of his work in this area, Euler published a table of binary logarithms of the integers from 1 to 8, to seven decimal digits of accuracy.[3][4]


In mathematics, the binary logarithm of a number n is written as log2 n. However, several other notations for this function have been used or proposed, especially in application areas.

Some authors write the binary logarithm as lg n.[5][6] Donald Knuth credits this notation to a suggestion of Edward Reingold,[7] but its use in both information theory and computer science dates to before Reingold was active.[8][9] The binary logarithm has also been written as log n with a prior statement that the default base for the logarithm is 2.[10][11][12]

Another notation that is sometimes used for the same function (especially in the German scientific literature) is ld n, from Latin logarithmus dualis.[13] The DIN 1302, ISO 31-11 and ISO 80000-2 standards recommend yet another notation, lb n. According to these standards, lg n should not be used for the binary logarithm, as it is instead reserved for log10 n.[14][15][16]


Information theory

The number of digits (bits) in the binary representation of a positive integer n is the integral part of 1 + log2 n, i.e.[6]

\lfloor \log_2 n\rfloor + 1. \,

In information theory, the definition of the amount of self-information and information entropy is often expressed with the binary logarithm, corresponding to making the bit the fundamental unit of information. However, the natural logarithm and the nat are also used in alternative notations for these definitions.[17]


A 16-player single elimination tournament bracket with the structure of a complete binary tree. The height of the tree (number of rounds of the tournament) equals the binary logarithm for complete binary trees with a number of leaves that is a power of two, and is larger otherwise.

Although the natural logarithm is more important than the binary logarithm in many areas of pure mathematics such as number theory and mathematical analysis, the binary logarithm has several applications in combinatorics:

  • Every binary tree with n leaves has height at least log2 n, with equality when n is a power of two and the tree is a complete binary tree.[18] Relatedly, the Strahler number of a river system with n tributary streams is at most log2 n + 1.[19]
  • Every family of sets with n different sets has at least log2 n elements in its union, with equality when the family is a power set.[20]
  • Every partial cube with n vertices has isometric dimension at least log2 n, and has at most 1/2 n log2 n edges, with equality when the partial cube is a hypercube graph.[21]
  • According to Ramsey's theorem, every n-vertex undirected graph has either a clique or an independent set of size logarithmic in n. The precise size that can be guaranteed is not known, but the best bounds known on its size involve binary logarithms. In particular, all graphs have a clique or independent set of size at least 1/2 log2 n (1 − o(1)) and almost all graphs do not have a clique or independent set of size larger than 2 log2 n (1 + o(1)).[22]
  • From a mathematical analysis of the Gilbert–Shannon–Reeds model of random shuffles, one can show that the number of times one needs to shuffle an n-card deck of cards, using riffle shuffles, to get a distribution on permutations that is close to uniformly random, is approximately 3/2 log2 n. This calculation forms the basis for a recommendation that 52-card decks should be shuffled seven times.[23]

Computational complexity

Binary search in a sorted array, an algorithm whose time complexity involves binary logarithms

The binary logarithm also frequently appears in the analysis of algorithms,[12] not only because of the frequent use of binary number arithmetic in algorithms, but also because binary logarithms occur in the analysis of algorithms based on two-way branching.[7] If a problem initially has n choices for its solution, and each iteration of the algorithm reduces the number of choices by a factor of two, then the number of iterations needed to select a single choice is again the integral part of log2 n. This idea is used in the analysis of several algorithms and data structures. For example, in binary search, the size of the problem to be solved is halved with each iteration, and therefore roughly log2 n iterations are needed to obtain a problem of size 1, which is solved easily in constant time. Similarly, a perfectly balanced binary search tree containing n elements has height 1 + log2 n.

However, the running time of an algorithm is usually expressed in big O notation, ignoring constant factors. Since log2 n = (logk n)/(logk 2), where k can be any number greater than 1, algorithms that run in O(log2 n) time can also be said to run in, say, O(log13 n) time. The base of the logarithm in expressions such as O(log n) or O(n log n) is therefore not important.[5] In other contexts, though, the base of the logarithm needs to be specified. For example O(2log2 n) is not the same as O(2ln n) because the former is equal to O(n) and the latter to O(n0.6931...).

Algorithms with running time O(n log n) are sometimes called linearithmic.[24] Some examples of algorithms with running time O(log n) or O(n log n) are:

Binary logarithms also occur in the exponents of the time bounds for some divide and conquer algorithms, such as the Karatsuba algorithm for multiplying n-bit numbers in time O(nlog2 3).[29]


A microarray of expression data for approximately 8700 genes. The relative expression rates of these genes are represented using binary logarithms.

In the analysis of microarray data in bioinformatics, expression rates of genes are often compared by using the binary logarithm of the ratio of expression rates. By using base 2 for the logarithm, a doubled expression rate can be described by a log ratio of 1, a halved expression rate can be described by a log ratio of −1, and an unchanged expression rate can be described by a log ratio of zero, for instance.[30] Data points obtained in this way are often visualized as a scatterplot in which one or both of the coordinate axes are binary logarithms of intensity ratios, or in visualizations such as the MA plot and RA plot that rotate and scale these log ratio scatterplots.[31]

Music theory

In music theory, the interval or perceptual difference between two tones is determined by the ratio of their frequencies. Intervals coming from rational number ratios with small numerators and denominators are perceived as particularly euphonius. The simplest and most important of these intervals is the octave, a frequency ratio of 2:1. The number of octaves by which two tones differ is the binary logarithm of their frequency ratio.[32]

To study tuning systems and other aspects of music theory that require finer distinctions between tones, it is helpful to have a measure of the size of an interval that is finer than an octave and is additive (as logarithms are) rather than multiplicative (as frequency ratios are). That is, if tones x, y, and z form a rising sequence of tones, then the measure of the interval from x to y plus the measure of the interval from y to z should equal the measure of the interval from x to z. Such a measure is given by the cent, which divides the octave into 1200 equal intervals (12 semitones of 100 cents each). Mathematically, given tones with frequencies f1 and f2, the number of cents in the interval from f1 to f2 is[32]


The millioctave is defined in the same way, but with a multiplier of 1000 instead of 1200.[33]

Sports scheduling

In competitive games and sports involving two players or teams in each game or match, the binary logarithm indicates the number of rounds necessary in a single-elimination tournament required to determine a winner. For example, a tournament of 4 players requires log2 4 = 2 rounds to determine the winner, a tournament of 32 teams requires log2 32 = 5 rounds, etc. In this case, for n players/teams where n is not a power of 2, log2 n is rounded up since it is necessary to have at least one round in which not all remaining competitors play. For example, log2 6 is approximately 2.585, which rounds up to 3, indicating that a tournament of 6 teams requires 3 rounds (either two teams sit out the first round, or one team sits out the second round). The same number of rounds is also necessary to determine a clear winner in a Swiss-system tournament.[34]


In photography, exposure values are measured in terms of the binary logarithm of the amount of light reaching the film or sensor, in accordance with the Weber–Fechner law describing a logarithmic response of the human visual system to light. A single stop of exposure is one unit on a base-2 logarithmic scale.[35][36] More precisely, the exposure value of a photograph is defined as

\log_2 \frac{N^2}{t}

where N is the f-number measuring the aperture of the lens during the exposure, and t is the number of seconds of exposure.

Binary logarithms (expressed as stops) are also used in densitometry, to express the dynamic range of light-sensitive materials or digital sensors.[37]


TI SR-50 scientific calculator (1974). The ln and log keys are in the second row; there is no log2 key.

Conversion from other bases

An easy way to calculate log2 n on calculators that do not have a log2 function is to use the natural logarithm (ln) or the common logarithm (log or log10) functions, which are found on most scientific calculators. The specific change of logarithm base formulae for this are:[36][38]

\log_2 n = \frac{\ln n}{\ln 2} = \frac{\log_{10} n}{\log_{10} 2},

or approximately

\log_2 n \approx 1.442695\ln n \approx 3.321928\log_{10} n.

Integer rounding

The binary logarithm can be made into a function from integers and to integers by rounding it up or down. These two forms of integer binary logarithm are related by this formula:

\lfloor \log_2(n) \rfloor = \lceil \log_2(n + 1) \rceil - 1, \text{ if }n \ge 1. [39]

The definition can be extended by defining \lfloor \log_2(0) \rfloor = -1. Extended in this way, this function is related to the number of leading zeros of the 32-bit unsigned binary representation of x, nlz(x).

\lfloor \log_2(n) \rfloor = 31 - \operatorname{nlz}(n).[39]

The integer binary logarithm can be interpreted as the zero-based index of the most significant 1 bit in the input. In this sense it is the complement of the find first set operation, which finds the index of the least significant 1 bit. Many hardware platforms include support for finding the number of leading zeros, or equivalent operations, which can be used to quickly find the binary logarithm; see find first set for details. The fls and flsl functions in the Linux kernel[40] and in some versions of the libc software library also compute the binary logarithm (rounded up to an integer, plus one).

Recursive approximation

For a general positive real number, the binary logarithm may be computed in two parts:[41]

  1. Compute the integer part, \lfloor\log_2 x\rfloor (called the characteristic of the logarithm)
  2. Compute the fractional part (the mantissa of the logarithm)

Computing the integer part is straightforward. For any x > 0, there exists a unique integer n such that 2nx < 2n+1, or equivalently 1 ≤ 2nx < 2. Now the integer part of the logarithm is simply n, and the fractional part is log2(2nx).[41] In other words:

\log_2 x = n + \log_2 y \quad\text{where } y = 2^{-n}x \text{ and } y \in [1,2)

The fractional part of the result is log2 y, and can be computed recursively, using only elementary multiplication and division.[41] To compute the fractional part:

  1. Start with a real number y in the half-open interval [1,2). If y = 1, then we are done and the fractional part is zero.
  2. Otherwise, square y repeatedly until the result z lies in the interval [2,4). Let m be the number of squarings needed. That is, z = y2m with m chosen such that m is in [2,4).
  3. Taking the logarithm of both sides and doing some algebra:
    \begin{align} \log_2 z &= 2^m \log_2 y \\ \log_2 y &= \frac{ \log_2 z }{ 2^m } \\ &= \frac{ 1 + \log_2(z/2) }{ 2^m } \\ &= 2^{-m} + 2^{-m}\log_2(z/2) \end{align}
  4. Once again z/2 is a real number in the interval [1,2). Return to step 1, and compute the binary logarithm of z/2 using the same method recursively.

The result of this is expressed by the following formulas, in which m_i is the number of squarings required in the i-th recursion of the algorithm:

\begin{align} \log_2 x &= n + 2^{-m_1} \left( 1 + 2^{-m_2} \left( 1 + 2^{-m_3} \left( 1 + \cdots \right)\right)\right) \\ &= n + 2^{-m_1} + 2^{-m_1-m_2} + 2^{-m_1-m_2-m_3} + \cdots \end{align}

In the special case where the fractional part in step 1 is found to be zero, this is a finite sequence terminating at some point. Otherwise, it is an infinite series that converges according to the ratio test, since each term is strictly less than the previous one (since every mi > 0). For practical use, this infinite series must be truncated to reach an approximate result. If the series is truncated after the ith term, then the error in the result is less than 2−(m1 + m2 + ... + mi).

An alternative algorithm that computes a single bit of the output in each iteration, using a sequence of shift and comparison operations to determine which bit to output, is also possible.[42]

Software library support

The log2 function is included in the standard C mathematical functions. The default version of this function takes double precision arguments but variants of it allow the argument to be single-precision or to be a long double.[43]


  1. ^ Groza, Vivian Shaw; Shelley, Susanne M. (1972), Precalculus mathematics, New York: Holt, Rinehart and Winston, p. 182,  .
  2. ^  . A copy of the same table with two more entries appears on p. 237, and another copy extended to negative powers appears on p. 249b.
  3. ^  .
  4. ^ Tegg, Thomas (1829), "Binary logarithms", London encyclopaedia; or, Universal dictionary of science, art, literature and practical mechanics: comprising a popular view of the present state of knowledge, Volume 4, pp. 142–143 .
  5. ^ a b  
  6. ^ a b  .
  7. ^ a b  , p. 11. The same notation was in the 1973 2nd edition of the same book (p. 23) but without the credit to Reingold.
  8. ^ Trucco, Ernesto (1956), "A note on the information content of graphs", Bull. Math. Biophys. 18: 129–135,  .
  9. ^ Mitchell, John N. (1962), "Computer multiplication and division using binary logarithms", IRE Transactions on Electronic Computers, EC-11 (4): 512–517,  .
  10. ^ Fiche, Georges; Hebuterne, Gerard (2013), Mathematics for Engineers, John Wiley & Sons, p. 152,  .
  11. ^ Cover, Thomas M.; Thomas, Joy A. (2012), Elements of Information Theory (2nd ed.), John Wiley & Sons, p. 33,  .
  12. ^ a b  
  13. ^ For instance, see Bauer, Friedrich L. (2009), Origins and Foundations of Computing: In Cooperation with Heinz Nixdorf MuseumsForum, Springer Science & Business Media, p. 54,  .
  14. ^ For DIN 1302 see Brockhaus Enzyklopädie in zwanzig Bänden 11, Wiesbaden: F.A. Brockhaus, 1970, p. 554,  .
  15. ^ For ISO 31-11 see Thompson, Ambler; Taylor, Barry M (March 2008), Guide for the Use of the International System of Units (SI) — NIST Special Publication 811, 2008 Edition — Second Printing (PDF),  .
  16. ^ For ISO 80000-2 see "Quantities and units – Part 2: Mathematical signs and symbols to be used in the natural sciences and technology" (PDF), International Standard ISO 80000-2 (1st ed.), December 1, 2009, Section 12, Exponential and logarithmic functions, p. 18 .
  17. ^ Van der Lubbe, Jan C. A. (1997), Information Theory, Cambridge University Press, p. 3,  .
  18. ^ Leiss, Ernst L. (2006), A Programmer's Companion to Algorithm Analysis, CRC Press, p. 28,  .
  19. ^  .
  20. ^ Equivalently, a family with k distinct elements has at most 2k distinct sets, with equality when it is a power set.
  21. ^  .
  22. ^  .
  23. ^  .
  24. ^ Sedgewick & Wayne (2011), p. 186.
  25. ^ Cormen et al., p. 156; Goodrich & Tamassia, p. 238.
  26. ^ Cormen et al., p. 276; Goodrich & Tamassia, p. 159.
  27. ^ Cormen et al., pp. 879–880; Goodrich & Tamassia, p. 464.
  28. ^ Edmonds, Jeff (2008), How to Think About Algorithms, Cambridge University Press, p. 302,  .
  29. ^ Cormen et al., p. 844; Goodrich & Tamassia, p. 279.
  30. ^ Causton, Helen; Quackenbush, John; Brazma, Alvis (2009), Microarray Gene Expression Data Analysis: A Beginner's Guide, John Wiley & Sons, pp. 49–50,  .
  31. ^ Eidhammer, Ingvar; Barsnes, Harald; Eide, Geir Egil; Martens, Lennart (2012), Computational and Statistical Methods for Protein Quantification by Mass Spectrometry, John Wiley & Sons, p. 105,  .
  32. ^ a b Campbell, Murray; Greated, Clive (1994), The Musician's Guide to Acoustics, Oxford University Press, p. 78,  .
  33. ^ Randel, Don Michael, ed. (2003), The Harvard Dictionary of Music (4th ed.), The Belknap Press of Harvard University Press, p. 416,  .
  34. ^ France, Robert (2008), Introduction to Physical Education and Sport Science, Cengage Learning, p. 282,  .
  35. ^ Allen, Elizabeth; Triantaphillidou, Sophie (2011), The Manual of Photography, Taylor & Francis, p. 228,  .
  36. ^ a b Davis, Phil (1998), Beyond the Zone System, CRC Press, p. 17,  .
  37. ^ Zwerman, Susan; Okun, Jeffrey A. (2012), Visual Effects Society Handbook: Workflow and Techniques, CRC Press, p. 205,  .
  38. ^ Bauer, Craig P. (2013), Secret History: The Story of Cryptology, CRC Press, p. 332,  .
  39. ^ a b Warren Jr., Henry S. (2002).  
  40. ^ fls, Linux kernel API,, retrieved 2010-10-17.
  41. ^ a b c Majithia, J. C.; Levan, D. (1973), "A note on base-2 logarithm computations", Proceedings of the IEEE 61 (10): 1519–1520,  .
  42. ^ Kostopoulos, D. K. (1991), "An algorithm for the computation of binary logarithms", IEEE Transactions on Computers 40 (11): 1267–1270,  .
  43. ^ " The log2 functions", ISO/IEC 9899:1999 specification (PDF), p. 226 .

External links

This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from World eBook Library are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.