Binary logarithm

From Wikipedia, the free encyclopedia
Jump to: navigation, search
Plot of log2 n

In mathematics, the binary logarithm (log2 n) is the logarithm to the base 2. It is the inverse function of n ↦ 2n. The binary logarithm of n is the power to which the number 2 must be raised to obtain the value n. This makes the binary logarithm useful for anything involving powers of 2, i.e. doubling. 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.


Information theory[edit]

The binary logarithm is often used in computer science and information theory because it is closely connected to the binary numeral system. In this context, it is frequently written lg n,[1] and another notation that is sometimes used for the same function (especially in German) is ld n, from Latin logarithmus duālis.[2] However, the ISO specification is that it should be lb (n), lg (n) being reserved for log10 n. The number of digits (bits) in the binary representation of a positive integer n is the integral part of 1 + lb n, i.e.

 \lfloor \operatorname{lb}\, n\rfloor + 1. \,

In information theory, the definition of the amount of self-information and information entropy involves the binary logarithm; this is needed because the unit of information, the bit, refers to information resulting from an occurrence of one of two equally probable alternatives.

Computational complexity[edit]

The binary logarithm also frequently appears in the analysis of algorithms. If a number n greater than 1 is divided by 2 repeatedly, the number of iterations needed to get a value at most 1 is again the integral part of lb 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 lb 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 lb n + 1.

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. In other contexts, though, the base of the logarithm needs to be specified. For example O(2lb 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 n lb n are sometimes called linearithmic. Some examples of algorithms with running time O(lb n) or O(n lb n) are:

Single-elimination tournaments[edit]

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

Using calculators[edit]

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

log2(n) = ln(n)/ln(2) = log(n)/log(2)


log2(n) = loge(n)×1.442695... = log10(n)×3.321928...

and this produces the curiosity that log2(n) is within 0.6% of loge(n) + log10(n). loge(n)+log10(n) is actually log2.0081359...(n) where the base is e1/(1+log10e) = 101/(1 + loge10) ≈ 2.00813 59293 46243 95422 87563 25191 0 to (32 significant figures). Of course, log1010 = logee = 1.



For integer domain and range, the binary logarithm can be computed rounding 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. [3]

The definition can be extended by defining  \lfloor \log_2(0) \rfloor = -1. 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).[3]

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. The article find first set contains more information on algorithms, architecture support, and applications for the integer binary logarithm.

Real number[edit]

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

  1. Compute the integer part, \lfloor\operatorname{lb}(x)\rfloor
  2. Compute the fractional part

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

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

The fractional part of the result is \operatorname{lb} y, and can be computed recursively, using only elementary multiplication and division. To compute the fractional part:

  1. We start with a real number y \in [1,2). If y=1, then we are done and the fractional part is zero.
  2. Otherwise, square y repeatedly until the result is z \in [2,4). Let m be the number of squarings needed. That is, z = y^{2\uparrow m} with m chosen such that z \in [2,4).
  3. Taking the logarithm of both sides and doing some algebra:
\operatorname{lb}\,z &= 2^m \operatorname{lb}\,y \\
\operatorname{lb}\,y &= \frac{ \operatorname{lb} z }{ 2^m } \\
&= \frac{ 1 + \operatorname{lb}(z/2) }{ 2^m } \\
&= 2^{-m} + 2^{-m}\operatorname{lb}(z/2)
  4. Notice that z/2 is once again a real number in the interval [1,2).
  5. 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:

\operatorname{lb}\,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

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 which converges according to the ratio test, since each term is strictly less than the previous one (since every m_i>0). For practical use, this infinite series must be truncated to reach an approximate result. If the series is truncated after the i-th term, then the error in the result is less than 2^{-(m_1+m_2+\cdots+m_i)}.

Fortunately, in practice we can do the computation and know the error margin without doing any algebra or any infinite series truncation. Suppose we want to compute the binary log of 1.65 with four binary digits. Repeat these steps four times:

  1. square the number
  2. if the square is >= 2, divide it by two and write a 1. Else write a 0.

The numbers we wrote are the logarithm written in binary. That will work when we start with any number between 1 and 2. So:

    • 1.65 squared is 2.72, which is more than two, so we halve it to 1.36 and write a 1
    • 1.36 squared is 1.85, less than two, so no halving, and write a 0
    • 1.85 squared is 3.43, more than two, so halve it to 1.72 and write a 1
    • 1.72 squared is 2.95. more than two, so write a 1 (no need to halve 2.95 because we are already done)

We wrote 1011 so far, so the binary logarithm of 1.65 written in binary is 0.1011 (or, written as a fraction, 11/16), and the error is less than 1/16. Adding 1/32, we get 23/32 which has error less than 1/32. In general, to get error less than 0.5 raised to the 1+N, we need N squarings and N or less halvings.

See also[edit]


  1. ^ Cormen, Thomas H.; Leiserson, Charles E., Rivest, Ronald L., Stein, Clifford (2001) [1990]. Introduction to Algorithms (2nd ed.). MIT Press and McGraw-Hill. p. 34. ISBN 0-262-03293-7. 
  2. ^ For instance, see Bauer, Friedrich L. (2009), Origins and Foundations of Computing: In Cooperation with Heinz Nixdorf MuseumsForum, Springer Science & Business Media, p. 54, ISBN 9783642029929 .
  3. ^ a b Warren Jr., Henry S. (2002). Hacker's Delight. Addison Wesley. p. 215. ISBN 978-0-201-91465-8.