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.
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, and another notation that is sometimes used for the same function (especially in German) is ld n, from Latin logarithmus duālis. 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.
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.
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 = (1/logk 2)logk n, 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:
- Average time quicksort and other comparison sort algorithms
- Searching in binary search trees
- Longest increasing subsequence
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).
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.
The definition can be extended by defining . This function is related to the number of leading zeros of the 32-bit unsigned binary representation of x, nlz(x).
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.
For a general positive real number, the binary logarithm may be computed in two parts:
- Compute the integer part,
- 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 ≤ 2−nx < 2. Now the integer part of the logarithm is simply n, and the fractional part is lb(2−nx). In other words:
The fractional part of the result is , and can be computed recursively, using only elementary multiplication and division. To compute the fractional part:
- We start with a real number . If , then we are done and the fractional part is zero.
- Otherwise, square repeatedly until the result is . Let be the number of squarings needed. That is, with chosen such that .
- Taking the logarithm of both sides and doing some algebra:
- Notice that is once again a real number in the interval .
- Return to step 1, and compute the binary logarithm of using the same method recursively.
The result of this is expressed by the following formulas, in which is the number of squarings required in the i-th recursion of the algorithm:
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 ). 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 .
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:
- square the number
- 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.
- Cormen, Thomas H.; Leiserson, Charles E., Rivest, Ronald L., Stein, Clifford (2001) . Introduction to Algorithms (2nd ed.). MIT Press and McGraw-Hill. p. 34. ISBN 0-262-03293-7.
- 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.
- Warren Jr., Henry S. (2002). Hacker's Delight. Addison Wesley. p. 215. ISBN 978-0-201-91465-8.