The binary logarithm is often used in computer science and information theory (where it is frequently written lg n, or ld n, from Latinlogarithmus dualis [although the ISO specification is that it should be lb (n), lg (n) being reserved for log10n] ), because it is closely connected to the binary numeral system. The number of digits (bits) in the binary representation of a positive integer n is the integral part of 1 + lg 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 lg 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 lg 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 lg n+1.
However, the running time of an algorithm is usually expressed in big O notation, ignoring constant factors. Since log2n = (1/logk 2)logkn, where k can be any number greater than 1, algorithms that run in O(log2n) time can also be said to run in, say, O(log13n) 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(2lg 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 lg n are sometimes called linearithmic. Some examples of algorithms with running time O(lg n) or O(n lg n) are:
and this produces the curiosity that log2(n) is within 0.6% of loge(n)+log10(n). loge(n)+log10(n) is actually log2.008135929...(n) where the base is elog10e(10)
For integer domain and range, the binary logarithm can be computed rounding up, or rounding down. These two forms of integer binary logarithm are related by this formula:
The following example code in the C language computes the binary logarithm (rounding down) of an integer, rounded down. The operator '>>' represents 'unsigned right shift'. The rounding down form of binary logarithm is identical to computing the position of the most significant 1 bit.
For a general real number, the binary logarithm may be computed in two parts:
Computing the integral part is straightforward. For any , there exists a unique integer such that , or equivalently . Now the integral part of the logarithm is simply , and the fractional part is . 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 .
Now square repeatedly until the result is . Call the result , and let be the number of squarings needed.
Now and so . Thus:
Notice that is once again a real number in the interval .
If (where is the desired precision), then and we are done. Otherwise, return to step 1, and compute the binary logarithm of using the same method recursively.
The result of all of this is:
After computing , the error in the result is less than .
The following Python computes the binary logarithm using the recursive method outlined above, showing the steps along the way:
Since Python does not optimize tail recursion, this can be implemented more efficiently with iteration. Here is an iterative version: