
Inmathematics, thebinary logarithm (log2 n) is thepower to which the number2 must beraised to obtain the value n. That is, for any real numberx,
For example, the binary logarithm of1 is0, the binary logarithm of2 is1, the binary logarithm of4 is 2, and the binary logarithm of32 is 5.
The binary logarithm is thelogarithm to the base2 and is theinverse function of thepower of two function. There are several alternatives to thelog2 notation for the binary logarithm; see theNotation section below.
Historically, the first application of binary logarithms was inmusic theory, byLeonhard Euler: the binary logarithm of a frequency ratio of two musical tones gives the number ofoctaves by which the tones differ. Binary logarithms can be used to calculate the length of the representation of a number in thebinary numeral system, or the number ofbits needed to encode a message ininformation theory. Incomputer science, they count the number of steps needed forbinary search and related algorithms. Other areasin which the binary logarithm is frequently used includecombinatorics,bioinformatics, the design of sportstournaments, andphotography.
Binary logarithms are included in the standardC mathematical functions and other mathematical software packages.

Thepowers of two have been known since antiquity; for instance, they appear inEuclid'sElements, Props. IX.32 (on thefactorization of powers of two) and IX.36 (half of theEuclid–Euler theorem, on the structure of evenperfect numbers).And the binary logarithm of a power of two is just its position in the ordered sequence of powers of two.On this basis,Michael Stifel has been credited with publishing the first known table of binary logarithms in 1544. His bookArithmetica Integra contains several tables that show theintegers with their corresponding powers of two. Reversing the rows of these tables allow them to be interpreted as tables of binary logarithms.[1][2]
Earlier than Stifel, the 8th centuryJain mathematicianVirasena is credited with a precursor to the binary logarithm. Virasena's concept ofardhacheda has been defined as the number of times a given number can be divided evenly by two. This definition gives rise to a function that coincides with the binary logarithm on the powers of two,[3] but it is different for other integers, giving the2-adic order rather than the logarithm.[4]
The modern form of a binary logarithm, applying to any number (not just powers of two) was considered explicitly byLeonhard Euler in 1739. Euler established the application of binary logarithms to music theory, long before their 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.[5][6]
The binary logarithm function may be defined as theinverse function to thepower of two function, which is a strictly increasing function over the positivereal numbers and therefore has a unique inverse.[7]Alternatively, it may be defined aslnn / ln 2, whereln is thenatural logarithm, defined in any of its standard ways. Using thecomplex logarithm in this definition allows the binary logarithm to be extended to thecomplex numbers.[8]
As with other logarithms, the binary logarithm obeys the following equations, which can be used to simplify formulas that combine binary logarithms with multiplication or exponentiation:[9]
For more, seelist of logarithmic identities.
In mathematics, the binary logarithm of a numbern is often written aslog2 n.[10] However, several other notations for this function have been used or proposed, especially in application areas.
Some authors write the binary logarithm aslgn,[11][12] the notation listed inThe Chicago Manual of Style.[13]Donald Knuth credits this notation to a suggestion ofEdward Reingold,[14] but its use in both information theory and computer science dates to before Reingold was active.[15][16] The binary logarithm has also been written aslogn with a prior statement that the default base for the logarithm is 2.[17][18][19] Another notation that is often used for the same function (especially in the German scientific literature) isldn,[20][21][22] fromLatinlogarithmusdualis[20] orlogarithmus dyadis.[20]TheDIN 1302 [de],ISO 31-11 andISO 80000-2 standards recommend yet another notation,lbn. According to these standards,lgn should not be used for the binary logarithm, as it is instead reserved for thecommon logarithmlog10n.[23][24][25]
The number of digits (bits) in thebinary representation of a positive integern is theintegral part of1 + log2 n, i.e.[12]
In information theory, the definition of the amount ofself-information andinformation entropy is often expressed with the binary logarithm, corresponding to making the bit the fundamentalunit of information. With these units, theShannon–Hartley theorem expresses the information capacity of a channel as the binary logarithm of its signal-to-noise ratio, plus one. However, thenatural logarithm and thenat are also used in alternative notations for these definitions.[26]

Although the natural logarithm is more important than the binary logarithm in many areas of pure mathematics such asnumber theory andmathematical analysis,[27] the binary logarithm has several applications incombinatorics:

The binary logarithm also frequently appears in theanalysis of algorithms,[19] 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.[14] If a problem initially hasn 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 oflog2 n. This idea is used in the analysis of severalalgorithms anddata structures. For example, inbinary search, the size of the problem to be solved is halved with each iteration, and therefore roughlylog2 n iterations are needed to obtain a solution for a problem of sizen.[34] Similarly, a perfectly balancedbinary search tree containingn elements has heightlog2(n + 1) − 1.[35]
The running time of an algorithm is usually expressed inbig O notation, which is used to simplify expressions by omitting their constant factors and lower-order terms. Because logarithms in different bases differ from each other only by a constant factor, algorithms that run inO(log2 n) time can also be said to run in, say,O(log13n) time. The base of the logarithm in expressions such asO(logn) orO(n logn) is therefore not important and can be omitted.[11][36] However, for logarithms that appear in the exponent of a time bound, the base of the logarithm cannot be omitted. For example,O(2log2 n) is not the same asO(2lnn) because the former is equal toO(n) and the latter toO(n0.6931...).
Algorithms with running timeO(n log n) are sometimes calledlinearithmic.[37] Some examples of algorithms with running timeO(logn) orO(n logn) are:
Binary logarithms also occur in the exponents of the time bounds for somedivide and conquer algorithms, such as theKaratsuba algorithm for multiplyingn-bit numbers in timeO(nlog2 3),[42]and theStrassen algorithm for multiplyingn ×n matrices in timeO(nlog2 7).[43] The occurrence of binary logarithms in these running times can be explained by reference to themaster theorem for divide-and-conquer recurrences.

Inbioinformatics,microarrays are used to measure how strongly different genes are expressed in a sample of biological material. Different rates of expression of a gene are often compared by using the binary logarithm of the ratio of expression rates: the log ratio of two expression rates is defined as the binary logarithm of the ratio of the two rates. Binary logarithms allow for a convenient comparison of expression rates: a doubled expression rate can be described by a log ratio of1, 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.[44]
Data points obtained in this way are often visualized as ascatterplot in which one or both of the coordinate axes are binary logarithms of intensity ratios, or in visualizations such as theMA plot andRA plot that rotate and scale these log ratio scatterplots.[45]
Inmusic theory, theinterval or perceptual difference between two tones is determined by the ratio of their frequencies. Intervals coming fromrational number ratios with small numerators and denominators are perceived as particularly euphonious. The simplest and most important of these intervals is theoctave, a frequency ratio of2:1. The number of octaves by which two tones differ is the binary logarithm of their frequency ratio.[46]
To studytuning 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 tonesx,y, andz form a rising sequence of tones, then the measure of the interval fromx toy plus the measure of the interval fromy toz should equal the measure of the interval fromx toz. Such a measure is given by thecent, which divides the octave into1200 equal intervals (12semitones of100 cents each). Mathematically, given tones with frequenciesf1 andf2, the number of cents in the interval fromf1 tof2 is[46]
Themillioctave is defined in the same way, but with a multiplier of1000 instead of1200.[47]
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 asingle-elimination tournament required to determine a winner. For example, a tournament of4 players requireslog2 4 = 2 rounds to determine the winner, a tournament of32 teams requireslog2 32 = 5 rounds, etc. In this case, forn players/teams wheren 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 approximately2.585, which rounds up to3, indicating that a tournament of6 teams requires3 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 aSwiss-system tournament.[48]
Inphotography,exposure values are measured in terms of the binary logarithm of the amount of light reaching the film or sensor, in accordance with theWeber–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.[49][50] More precisely, the exposure value of a photograph is defined as
whereN is thef-number measuring theaperture of the lens during the exposure, andt is the number of seconds of exposure.[51]
Binary logarithms (expressed as stops) are also used indensitometry, to express thedynamic range of light-sensitive materials or digital sensors.[52]

An easy way to calculatelog2 n oncalculators that do not have alog2 function is to use thenatural logarithm (ln) or thecommon logarithm (log orlog10) functions, which are found on mostscientific calculators. Tochange the logarithm base to 2 frome,10, or any other baseb, one can use theformulae:[50][53]
The binary logarithm can be made into a function from integers and to integers byrounding it up or down. These two forms of integer binary logarithm are related by this formula:
The definition can be extended by defining. Extended in this way, this function is related to thenumber of leading zeros of the 32-bit unsigned binary representation ofx,nlz(x).
The integer binary logarithm can be interpreted as the zero-based index of the most significant1 bit in the input. In this sense it is the complement of thefind first set operation, which finds the index of the least significant1 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. Thefls andflsl functions in theLinux kernel[57] and in some versions of thelibc software library also compute the binary logarithm (rounded up to an integer, plus one).
For a number represented infloating point as, with integer exponent andmantissa in the range, the binary logarithm can be roughly approximated as.[16] This approximation is exact at both ends of the range of mantissas but underestimates the logarithm in the middle of the range, reaching a maximum error of approximately 0.086 at a mantissa of approximately 0.44. It can be made more accurate by using apiecewise linear function of,[58] or more crudely by adding a constant correction term. For instance choosing would halve the maximum error. Thefast inverse square root algorithm uses this idea, with a different correction term that can be inferred to be, by directly manipulating the binary representation of to multiply this approximate logarithm by, obtaining a floating point value that approximates.[59]
For a generalpositive real number, the binary logarithm may be computed in two parts.[60]First, one computes theinteger part, (called the characteristic of the logarithm).This reduces the problem to one where the argument of the logarithm is in a restricted range, the interval[1, 2), simplifying the second step of computing the fractional part (the mantissa of the logarithm).For anyx > 0, there exists a unique integern such that2n ≤x < 2n+1, or equivalently1 ≤ 2−nx < 2. Now the integer part of the logarithm is simplyn, and the fractional part islog2(2−nx).[60] In other words:
For normalizedfloating-point numbers, the integer part is given by the floating-point exponent,[61] and for integers it can be determined by performing acount leading zeros operation.[62]
The fractional part of the result islog2y and can be computed iteratively, using only elementary multiplication and division.[60]The algorithm for computing the fractional part can be described inpseudocode as follows:
The result of this is expressed by the following recursive formulas, in which is the number of squarings required in thei-th iteration of the algorithm:
In the special case where the fractional part in step 1 is found to be zero, this is afinite sequence terminating at some point. Otherwise, it is aninfinite series thatconverges according to theratio test, since each term is strictly less than the previous one (since everymi > 0). SeeHorner's method. For practical use, this infinite series must be truncated to reach an approximate result. If the series is truncated after thei-th term, then the error in the result is less than2−(m1 +m2 + ⋯ +mi).[60]
Thelog2 function is included in the standardC mathematical functions. The default version of this function takesdouble precision arguments but variants of it allow the argument to be single-precision or to be along double.[63] In computing environments supportingcomplex numbers and implicit type conversion such asMATLAB the argument to thelog2 function is allowed to be anegative number, returning a complex one.[64]
IMLOG2 function for complex binary logarithms: seeBourg, David M. (2006),Excel Scientific and Engineering Cookbook, O'Reilly Media, p. 232,ISBN 978-0-596-55317-3.In the following, and unless otherwise stated, the notationlogx always stands for the logarithm to the base2 ofx.
Unless otherwise specified, we will take all logarithms to base2.
One of the interesting and sometimes even surprising aspects of the analysis of data structures and algorithms is the ubiquitous presence of logarithms ... As is the custom in the computing literature, we omit writing the baseb of the logarithm whenb = 2.
in advanced mathematics and science the only logarithm of importance is the natural logarithm.
{{cite web}}: CS1 maint: overridden setting (link)