# 二进制对数

二进制对数是以 2 为底的对数：对于正实数 x，它是满足 \( 2^{y} = x \) 的指数 y，记作 \( \log_{2} x \)<sup>[1](https://oeis.org/wiki/Binary_logarithm)</sup>。它计算的是一个数必须被翻倍或减半多少次才能达到另一个数，这使它成为计算领域自然而然的对数选择——在计算中，信息以二进制数字存储，算法反复将问题一分为二<sup>[2](https://opendsax.cs.vt.edu/OpenDSA/Books/CS3/html/Logarithms.html)</sup>。

| 关键事实 | 详情 |
|---|---|
| 定义 | 当且仅当 \( 2^{y} = x \) 时，\( \log_{2} x = y \)，其中 \( x > 0 \)<sup>[1](https://oeis.org/wiki/Binary_logarithm)</sup> |
| 换底公式 | \( \log_{2} x = \ln x / \ln 2 \)，其中 \( \ln 2 \approx 0.69314718 \)<sup>[1](https://oeis.org/wiki/Binary_logarithm)</sup> |
| 示例值 | \( \log_{2} \pi \approx 1.65149612947 \)<sup>[1](https://oeis.org/wiki/Binary_logarithm)</sup> |
| 整数值 | 仅当 x 是 2 的整数次幂或其倒数时，\( \log_{2} x \) 才是整数<sup>[1](https://oeis.org/wiki/Binary_logarithm)</sup> |
| 信息单位 | 以 2 为底计算的熵和信息以比特为度量单位；以自然对数计算时单位为奈特<sup>[3](https://mathworld.wolfram.com/BinaryLogarithm.html)</sup> |
| 标准库 | C99 提供 log2、log2f 和 log2l；Python 的 math.log2 在 3.3 版本中加入<sup>[4](https://en.cppreference.com/w/c/numeric/math/log2.html)</sup><sup> • </sup><sup>[5](https://docs.python.org/3/library/math.html?highlight=dist)</sup> |
| 算法中的作用 | 对 n 个元素进行二分查找需要 \( \lceil \log_{2} n \rceil \) 次减半；\( \lfloor \log_{2} n \rfloor + 1 \) 是用二进制写出 n 所需的位数<sup>[2](https://opendsax.cs.vt.edu/OpenDSA/Books/CS3/html/Logarithms.html)</sup><sup> • </sup><sup>[6](https://www.ime.usp.br/~pf/algorithms/chapters/footnotes/math.html)</sup> |

## 定义与基本性质

二进制对数继承了对数的一般法则。对于正值，乘积的对数是对数的和，商的对数是对数的差，r 次幂的对数是 r 乘以对数<sup>[2](https://opendsax.cs.vt.edu/OpenDSA/Books/CS3/html/Logarithms.html)</sup>。有两个推论在分析中被反复使用。第一，\( 2^{\log_{2} n} = n \)，因为同底的对数和指数函数互为逆运算。第二，不同底的对数仅相差一个常数因子：\( \log_{a} n = \log_{b} n / \log_{b} a \)，这就是为什么渐近运行时间分析中写作 \( \log n \) 而不指定底数<sup>[2](https://opendsax.cs.vt.edu/OpenDSA/Books/CS3/html/Logarithms.html)</sup>。

由于将一个数翻倍恰好使其二进制对数增加 1，该函数增长缓慢：\( \log_{2} 1000 \) 仍然不到 10<sup>[7](https://mathbyfirstprinciples.org/algebra-2/exponential-and-logarithmic-functions/logarithms/)</sup>。该函数仅对正自变量有定义；负数或零输入没有实数二进制对数<sup>[7](https://mathbyfirstprinciples.org/algebra-2/exponential-and-logarithmic-functions/logarithms/)</sup>。

## 记号

三种记号并存。\( \log_{2} x \) 含义明确，为清晰起见首选此写法<sup>[1](https://oeis.org/wiki/Binary_logarithm)</sup>。缩写 lg x 出现在数论文献中，但不鼓励将其作为二进制对数的名称，因为俄罗斯和德国文献用 lg 表示以 10 为底的对数——这一用法得到了美国商务部的推荐；一些德国文献反而用 ld x 表示二进制对数<sup>[3](https://mathworld.wolfram.com/BinaryLogarithm.html)</sup>。在算法分析中，不加修饰的 log n 通常指 \( \log_{2} n \)，或者底数无关紧要，因为常数因子被忽略<sup>[2](https://opendsax.cs.vt.edu/OpenDSA/Books/CS3/html/Logarithms.html)</sup>。

## 比特、信息论与计数

二进制对数以比特为单位度量信息。当克劳德·香农（Claude Shannon）在 1948 年定义其信息度量时，他选择了以 2 为底，而更早的拉尔夫·哈特利（Ralph Hartley）选择了以 10 为底；以 2 为底时，得知一次公平抛硬币的结果携带 \( \log_{2} 2 = 1 \) 比特的信息<sup>[8](https://ocw.mit.edu/courses/6-02-introduction-to-eecs-ii-digital-communication-systems-fall-2012/e3aafb9534ab3b0e1731165f823004de_BtaVq2g17G0.pdf)</sup>。香农的熵公式——信源的期望信息——用 \( \log_{2} \) 写出，使其单位为比特<sup>[8](https://ocw.mit.edu/courses/6-02-introduction-to-eecs-ii-digital-communication-systems-fall-2012/e3aafb9534ab3b0e1731165f823004de_BtaVq2g17G0.pdf)</sup>。改用自然对数则得到以奈特为单位的相同量<sup>[3](https://mathworld.wolfram.com/BinaryLogarithm.html)</sup>。

同一函数也用于计算表示大小。n 的二进制表示中的位数本质上是 \( \log_{2} n \)，约为十进制位数的 3.3 倍；更精确地说，需要 \( \lfloor \log_{2} n \rfloor + 1 \) 位<sup>[6](https://www.ime.usp.br/~pf/algorithms/chapters/footnotes/math.html)</sup>。对于正整数 n，其最高有效 1 位的零基索引为 ⌊log₂ n⌋<sup>[4](https://en.cppreference.com/w/c/numeric/math/log2.html)</sup><sup> • </sup><sup>[1](https://oeis.org/wiki/Binary_logarithm)</sup>。

## 算法与复杂度

分治算法以对数步数运行。二分查找通过反复将搜索区间减半来在有序数组中定位一个值，需要 \( \lceil \log_{2} n \rceil \) 次减半才能将 n 个元素缩减为一个<sup>[2](https://opendsax.cs.vt.edu/OpenDSA/Books/CS3/html/Logarithms.html)</sup>。数据结构教材几乎只使用以 2 为底的对数，因为算法最常将事物一分为二或以二进制位存储编码<sup>[2](https://opendsax.cs.vt.edu/OpenDSA/Books/CS3/html/Logarithms.html)</sup>。搜索策略的比较量化了这一差距：二分查找的复杂度为 \( \log_{2} N \)，而线性查找的复杂度为 N，随着数据增长，增长更慢的函数胜出<sup>[9](https://www.cs.toronto.edu/~strider/docs/ICS_Chapter_4.pdf)</sup>。

硬件实现以组合电路的形式存在。一种已提出的电路在 \( O(\log N) \) 时间内使用 \( O(N^{2}) \) 面积计算 N 位数的二进制对数，同一结构还可计算反对数，从而能在 \( O(\log N) \) 时间内获得两个二进制数的商<sup>[10](https://www.sciencedirect.com/science/article/pii/0165168487900478)</sup>。

## 软件支持

C 语言自 C99 起就包含了以 2 为底的对数函数：用于 double 的 log2、用于 float 的 log2f、用于 long double 的 log2l，以及 tgmath.h 中的类型泛型宏<sup>[4](https://en.cppreference.com/w/c/numeric/math/log2.html)</sup>。错误处理遵循惯例：负自变量引发域错误（在支持的环境中返回 NaN），零引发极点错误并返回 −HUGE_VAL，log2(1) 返回 +0，+∞ 的 log2 返回 +∞<sup>[4](https://en.cppreference.com/w/c/numeric/math/log2.html)</sup><sup> • </sup><sup>[11](https://www.man7.org/linux/man-pages/man3/log2f.3p.html)</sup>。Python 提供 math.log2，在 3.3 版本中加入，通常比通过通用双参数形式计算 log(x, 2) 更精确<sup>[5](https://docs.python.org/3/library/math.html?highlight=dist)</sup>。由于 \( \log_{2} x = \ln x / \ln 2 \)，任何缺少专用函数的环境都可以通过将自然对数除以常数 \( \ln 2 \) 来构造这样的函数<sup>[1](https://oeis.org/wiki/Binary_logarithm)</sup>。

## 历史

对数由梅奇斯顿男爵约翰·纳皮尔（John Napier）引入，他于 1614 年发表了对数表；这个词本身来自希腊语 logos（比率）和 arithmos（数）<sup>[12](https://www.nms.ac.uk/discover-catalogue/napiers-bones-the-logarithmic-genius-who-revolutionised-calculations)</sup>。纳皮尔自己的对数并不以现代意义上的任何数为底，尽管可以粗略地描述为以 \( 1/e \) 为底，它们涉及一个与他那个时代七位正弦表相关的比例常数 \( 10^{7} \)<sup>[13](https://mathshistory.st-andrews.ac.uk/Biographies/Napier/)</sup>。格雷沙姆学院（Gresham College）的亨利·布里格斯（Henry Briggs）于 1615 年拜访了纳皮尔，改用满足 log 1 = 0 的对数这一转变正出自他们的讨论；布里格斯于 1617 年发表了第一批以 10 为底的对数<sup>[12](https://www.nms.ac.uk/discover-catalogue/napiers-bones-the-logarithmic-genius-who-revolutionised-calculations)</sup><sup> • </sup><sup>[13](https://mathshistory.st-andrews.ac.uk/Biographies/Napier/)</sup>。

纳皮尔与二进制对数如今所属的计算领域还有第二重联系：他是第一个提出在计算中使用二进制数的人<sup>[12](https://www.nms.ac.uk/discover-catalogue/napiers-bones-the-logarithmic-genius-who-revolutionised-calculations)</sup>。二进制对数本身早于计算技术作为一门研究领域的存在。莱昂哈德·欧拉（Leonhard Euler）于 1739 年将二进制对数应用于音乐理论，远在信息论和计算机科学诞生之前；十七世纪的数学家——包括笛卡尔（Descartes）、牛顿（Newton）和尼古拉斯·墨卡托（Nicolaus Mercator）——使用对数来为音程赋予定量度量<sup>[14](https://ora.ox.ac.uk/objects/uuid%3A7da17bf4-a88a-4366-b66c-690db9914486)</sup>。

## References

1. [Binary logarithm, OeisWiki](https://oeis.org/wiki/Binary_logarithm)
2. [Logarithms, CS3 Data Structures & Algorithms, OpenDSA](https://opendsax.cs.vt.edu/OpenDSA/Books/CS3/html/Logarithms.html)
3. [Binary Logarithm, Wolfram MathWorld](https://mathworld.wolfram.com/BinaryLogarithm.html)
4. [log2, log2f, log2l, cppreference.com](https://en.cppreference.com/w/c/numeric/math/log2.html)
5. [math, Mathematical functions, Python 3 documentation](https://docs.python.org/3/library/math.html?highlight=dist)
6. [Footnotes on mathematics in computer science, Institute of Mathematics and Statistics, University of São Paulo](https://www.ime.usp.br/~pf/algorithms/chapters/footnotes/math.html)
7. [Introduction to Logarithms, Math by First Principles](https://mathbyfirstprinciples.org/algebra-2/exponential-and-logarithmic-functions/logarithms/)
8. [Overview: information and entropy, MIT OCW 6.02 (Fall 2012)](https://ocw.mit.edu/courses/6-02-introduction-to-eecs-ii-digital-communication-systems-fall-2012/e3aafb9534ab3b0e1731165f823004de_BtaVq2g17G0.pdf)
9. [Computational Complexity, University of Toronto course notes (F. Estrada, 2024)](https://www.cs.toronto.edu/~strider/docs/ICS_Chapter_4.pdf)
10. [A new design approach to binary logarithm computation, Signal Processing (ScienceDirect)](https://www.sciencedirect.com/science/article/pii/0165168487900478)
11. [log2(3p), Linux manual page, POSIX.1-2017](https://www.man7.org/linux/man-pages/man3/log2f.3p.html)
12. [Napier's bones: The logarithmic genius who revolutionised calculations, National Museums Scotland](https://www.nms.ac.uk/discover-catalogue/napiers-bones-the-logarithmic-genius-who-revolutionised-calculations)
13. [John Napier (1550–1617), MacTutor History of Mathematics](https://mathshistory.st-andrews.ac.uk/Biographies/Napier/)
14. [Musical logarithms in the seventeenth century: Descartes, Mercator, Newton, Oxford University Research Archive](https://ora.ox.ac.uk/objects/uuid%3A7da17bf4-a88a-4366-b66c-690db9914486)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Oct 15, 2026; Oct 25, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Arithmetic and number systems › Elementary and formal arithmetic › Elementary arithmetic operations

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/binary-logarithm

English original: https://www.edgepedia.org/binary-logarithm
