# 位图

位图（bitmap，又称位集或位向量）是一种数据结构，它将一组整数表示为一个比特数组，当且仅当元素 i 属于该集合时，第 i 个比特位被置为 1。这种表示是精确的，而非概率性的，它支持常数时间内的成员测试、插入和删除，并且集合运算可直接以位级机器指令实现。<sup>[1](https://github.com/gi-dellav/book_of_algorithmics/blob/main/src/data-structures/bitset.md)</sup>

| 关键事实 | 详情 |
|---|---|
| 基本操作 | 设置、清除和测试一个比特均为 O(1)；典型实现使用一次移位、一个掩码和一次内存访问。<sup>[1](https://github.com/gi-dellav/book_of_algorithmics/blob/main/src/data-structures/bitset.md)</sup> |
| 集合代数 | 并集为按位或，交集为按位与，差集为与另一集合按位取反后的结果相与，逐字应用于整个数组。<sup>[2](https://opendsa-server.cs.vt.edu/ODSA/Books/pubbook/odsa-all/fall-2019/Public_Instance/html/SetSearch.html)</sup> |
| 空间开销 | 每个可能元素占一个比特：范围 [0, 10<sup>7</sup>) 上的位图约占 1.25 MB，而容纳一百万个整数的平衡树约占 32 MB。<sup>[1](https://github.com/gi-dellav/book_of_algorithmics/blob/main/src/data-structures/bitset.md)</sup> |
| 压缩形式 | Roaring 位图将 32 位空间划分为 2<sup>16</sup> 个整数的块，每块存储为有序数组（至多 4096 个元素）、65536 位的位图，或排除值的逆数组（61140 个及以上）。<sup>[3](https://roaringbitmap.readthedocs.io/en/latest/)</sup> |
| 数据库用途 | 位图索引为每个不同键值存储一个位图，每行一个比特；它们适合不同值相对行数较少的数据仓库列。<sup>[4](https://docs.oracle.com/database/121/DWHSG/schemas.htm)</sup> |
| 简洁查询 | 支持 rank 查询与 select 查询的结构以 O(1) 时间和 o(n) 额外空间回答“位置 i 之前有多少个 1 比特”和“第 j 个 1 比特在哪里”；最小的实用结构增加约 3.5% 至 4% 的开销。<sup>[5](https://arxiv.org/abs/2206.01149)</sup> |
| 文件系统用途 | ext4 用位图跟踪数据块和 inode 的使用情况，块组内每个块或 inode 表项占一个比特。<sup>[6](https://www.kernel.org/doc/html/v7.2/filesystems/ext4/bitmaps.html)</sup> |

## 表示与基本操作

在朴素形式中，位图是一个机器字数组。设置元素 x 意味着将索引为 x 除以字长的字与左移 x 模字长位的 1 进行按位或；测试则是相应的掩码读取。每次操作编译为约三条指令，在一项已发表的测量中，[Zen 2](https://www.edgepedia.org/zh/zen-2) 处理器上每次操作约 3 纳秒。<sup>[1](https://github.com/gi-dellav/book_of_algorithmics/blob/main/src/data-structures/bitset.md)</sup>

主要优势体现在集合代数上。如果一个集合能装入单个计算机字，并集、交集和差集就是单条按位指令；更大的集合则逐字处理。例如，0 到 15 之间既是素数又是奇数的整数，可通过将两个对应的 16 位模式相与得到。<sup>[2](https://opendsa-server.cs.vt.edu/ODSA/Books/pubbook/odsa-all/fall-2019/Public_Instance/html/SetSearch.html)</sup> 硬件支持进一步强化了这一点：SIMD 指令加速批量操作，popcount 指令给出一个字中 1 的个数，尾零指令（如延迟为 3 个周期的 tzcnt）使迭代可以通过每步清除最低置位比特来跳过整字为零的字。<sup>[1](https://github.com/gi-dellav/book_of_algorithmics/blob/main/src/data-structures/bitset.md)</sup>

权衡在于密度。朴素位图无论存在多少元素都为整个值域分配空间，因此对稠密集合（占用率高于约 1%）有利，对稀疏集合不利，后者更适合压缩格式。<sup>[1](https://github.com/gi-dellav/book_of_algorithmics/blob/main/src/data-structures/bitset.md)</sup> 标准库直接暴露朴素形式：C++ 提供 std::bitset，一个固定大小的容器，具有 set、reset、flip、count 和 test 操作以及按位运算符，还提供 std::vector&lt;bool&gt; 作为动态大小的特化，实现通常按每元素一比特打包。<sup>[7](https://learn.microsoft.com/en-us/cpp/standard-library/bitset-class?view=msvc-170)</sup>

## 压缩位图：Roaring

对于大值域上的稀疏或混合密度集合，压缩位图格式在保持快速位级接口的同时，只存储值域中相关的部分。最广泛采用的是 Roaring 位图，一种混合设计，将 32 位整数空间划分为 2<sup>16</sup> 个整数的块，并按密度为每块选择容器表示：至多 4096 个元素的块使用 16 位值的有序数组，更稠密的块使用固定的 65536 位位图，而对于元素数达到 61140 个及以上的近乎满块，则使用一个存储*不在*集合中的值的数组。<sup>[3](https://roaringbitmap.readthedocs.io/en/latest/)</sup>

Roaring 由萨米·尚比、丹尼尔·勒米尔、欧文·卡泽和罗伯特·戈丹提出，2016 年发表于《软件：实践与经验》。<sup>[8](https://roaringbitmap.org/publications/)</sup> 与 WAH、Concise 和 EWAH 等主要依赖游程编码的早期方案不同，Roaring 在两级树内混合了未压缩位图和紧凑数组，使操作在无序数据上保持快速。作者报告的基准测试发现，该格式比这些基于游程的替代方案快数倍，最高可达两个数量级，同时压缩效果也更好；后来的修订版为具有长可压缩游程的数据增加了游程编码容器。<sup>[9](https://arxiv.org/abs/1603.06549)</sup>

该格式在 C/C++（CRoaring，针对 AVX2、AVX-512 和 NEON 有 SIMD 优化）、Java、Go、Rust、Python 及其他语言中实现，序列化格式规范支持实现之间的互操作。<sup>[10](https://github.com/RoaringBitmap/CRoaring)</sup><sup> • </sup><sup>[11](https://github.com/RoaringBitmap/RoaringBitmap/blob/master/README.md)</sup> C 库同时提供 32 位和 64 位 API；64 位变体在自适应基数树中按 48 位前缀索引 16 位容器。<sup>[10](https://github.com/RoaringBitmap/CRoaring)</sup> 使用 Roaring 位图的生产系统包括 [Elasticsearch](https://www.edgepedia.org/zh/elasticsearch)、Apache Spark、Netflix 的 Atlas、LinkedIn 的 Pinot、Metamarkets 的 Druid、Pilosa、Apache Hive、Apache Tez、Microsoft Visual Studio Team Services 和 [Apache Kylin](https://www.edgepedia.org/zh/apache-kylin)。<sup>[12](https://arxiv.org/abs/1709.07821)</sup>

## 数据库中的位图索引

[位图索引](https://www.edgepedia.org/zh/bitmap-index)将该结构应用于查询处理：对于被索引列的每个不同值，数据库存储一个位图，每行一个比特，在该行包含该值的位置置位。映射函数将比特位置转换回行标识符。Oracle 文档将位图索引描述为广泛用于数据仓库环境，这类环境数据量大、查询为即席查询但并发更新少，并指出它们通常只占被索引数据的一小部分，而完全覆盖的 B 树索引可能比表大数倍。<sup>[4](https://docs.oracle.com/database/121/DWHSG/schemas.htm)</sup>

经典指导原则是位图索引适合低基数（不同值相对行数比例小）的列；只有两个值的性别列是教科书案例，Oracle 还指出一百万行表上有一万个不同值的列仍可作为候选。WHERE 子句中含多个条件的查询受益最大，因为 AND 和 OR 条件在任何表访问之前就直接通过位图上的布尔运算解决。<sup>[4](https://docs.oracle.com/database/121/DWHSG/schemas.htm)</sup> 位图索引还包含 NULL 值的行，而 B 树索引不存储这些行，因此可用于对可空列进行 COUNT 查询。<sup>[4](https://docs.oracle.com/database/121/DWHSG/schemas.htm)</sup>

低基数规则有一个有据可查的例外。[劳伦斯伯克利国家实验室](https://www.edgepedia.org/zh/lawrence-berkeley-national-laboratory)的吴克胜及其同事的分析表明，压缩位图索引对高基数属性仍然高效：使用字对齐混合压缩（WAH）时，即使在高基数下索引大小相对于 B 树索引仍保持较小，并且 WAH 压缩索引回答查询的时间约为字节对齐位图编码（BBC）压缩索引的一半，同时在他们的测试中始终优于投影索引。<sup>[13](https://www.vldb.org/conf/2004/RS1P2.PDF)</sup> 该技术的历史比关系型数据仓库更早：位图索引出现在 Model 204 引擎中，该引擎于 1972 年为 IBM 370 商业化，而“位图索引”这一名称因帕特里克·奥尼尔 1987 年对该系统的描述而流行。<sup>[14](https://www.sciencedirect.com/science/article/abs/pii/S0169023X09001256)</sup>

现代分析引擎延续了这一模式。[Apache Paimon](https://www.edgepedia.org/zh/apache-paimon) 将其全局位图索引的每个位图块存储为序列化的 RoaringNavigableMap64，[Apache Doris](https://www.edgepedia.org/zh/apache-doris) 定义了自己的位图二进制格式，用一个标志字节区分 32 位 Roaring 载荷、64 位载荷和哈希集。<sup>[15](https://paimon.apache.org/docs/master/multimodal-table/global-index/bitmap/)</sup><sup> • </sup><sup>[16](https://doris.apache.org/community/developer-guide/bitmap-hll-file-format/)</sup>

## 位向量上的 rank 查询与 select 查询

除成员测试之外，还有两种查询使位图成为简洁数据结构的构建模块，这类结构以接近信息论下界的方式表示数据，同时仍支持快速操作。给定位置 i，rank 查询返回该位置之前的 1 比特数；给定 j，select 查询返回第 j 个 1 比特的位置。在长度为 n 的位向量上，两者均可在 O(1) 时间内用 o(n) 比特的额外空间回答，实现方式是预计算块计数的层次结构加上 popcount 指令。<sup>[5](https://arxiv.org/abs/2206.01149)</sup>

这些结构的实际工程是一个活跃领域。最节省空间的未压缩结构是周及其同事的 cs-poppy，使用约 3.51% 的额外空间；[卡尔斯鲁厄理工学院](https://www.edgepedia.org/zh/karlsruhe-institute-of-technology)算法与数据结构研究员弗洛里安·库尔皮奇 于 2022 年提出的结构在匹配该开销的同时，rank 查询最多快 8%，select 查询最多快 16.5%。<sup>[5](https://arxiv.org/abs/2206.01149)</sup> SPIDER 结构（SEA 2024）使用 3.82% 的额外空间，在 80 亿比特及以上的数据集上实现了已知最佳的 rank 查询时间，它利用机器学习预测几乎消除了 select 查询原本所需的线性扫描。<sup>[17](https://drops.dagstuhl.de/entities/document/10.4230/LIPIcs.SEA.2024.21)</sup> 这些结构是小波树、Elias-Fano 整数编码、图表示和文本索引的基础。<sup>[5](https://arxiv.org/abs/2206.01149)</sup>

## 其他应用

文件系统使用位图进行分配。在 ext4 中，数据块位图跟踪每个块组内数据块的使用情况，inode 位图记录哪些 inode 表项正在使用，每块或每项一个比特；这种大小关系意味着一个块组容纳的块数是逻辑块字节数的 8 倍。<sup>[6](https://www.kernel.org/doc/html/v7.2/filesystems/ext4/bitmaps.html)</sup> 文档检索系统应用同样的思路，为每个关键词存储一个位向量，将多个关键词的向量相与以找到包含全部关键词的文档。<sup>[2](https://opendsa-server.cs.vt.edu/ODSA/Books/pubbook/odsa-all/fall-2019/Public_Instance/html/SetSearch.html)</sup>

## References

1. [Bitmap Structures, book_of_algorithmics](https://github.com/gi-dellav/book_of_algorithmics/blob/main/src/data-structures/bitset.md)
2. [22.5. Bit Vectors for Representing Sets, OpenDSA](https://opendsa-server.cs.vt.edu/ODSA/Books/pubbook/odsa-all/fall-2019/Public_Instance/html/SetSearch.html)
3. [RoaringBitmap API documentation](https://roaringbitmap.readthedocs.io/en/latest/)
4. [Data Warehousing Optimizations and Techniques, Oracle Database 12c documentation](https://docs.oracle.com/database/121/DWHSG/schemas.htm)
5. [Florian Kurpicz (2022). Engineering Compact Data Structures for Rank and Select Queries on Bit Vectors. arXiv.](https://arxiv.org/abs/2206.01149)
6. [3.3. Block and inode Bitmaps, The Linux Kernel documentation](https://www.kernel.org/doc/html/v7.2/filesystems/ext4/bitmaps.html)
7. [bitset Class, Microsoft Learn C++ reference](https://learn.microsoft.com/en-us/cpp/standard-library/bitset-class?view=msvc-170)
8. [Roaring Bitmaps publications page](https://roaringbitmap.org/publications/)
9. [Daniel Lemire, Gregory Ssi-Yan-Kai, Owen Kaser (2016). Consistently faster and smaller compressed bitmaps with Roaring. arXiv.](https://arxiv.org/abs/1603.06549)
10. [RoaringBitmap/CRoaring: Roaring bitmaps in C (and C++)](https://github.com/RoaringBitmap/CRoaring)
11. [RoaringBitmap/RoaringBitmap README](https://github.com/RoaringBitmap/RoaringBitmap/blob/master/README.md)
12. [Daniel Lemire and colleagues (2017). Roaring Bitmaps: Implementation of an Optimized Software Library. arXiv.](https://arxiv.org/abs/1709.07821)
13. [Wu, Stockinger and colleagues (2004). On the performance of bitmap indices for high cardinality attributes. VLDB.](https://www.vldb.org/conf/2004/RS1P2.PDF)
14. [Sorting improves word-aligned bitmap indexes, Information Systems (ScienceDirect)](https://www.sciencedirect.com/science/article/abs/pii/S0169023X09001256)
15. [Bitmap Index, Apache Paimon documentation](https://paimon.apache.org/docs/master/multimodal-table/global-index/bitmap/)
16. [Bitmap and HLL Binary Format Specification, Apache Doris](https://doris.apache.org/community/developer-guide/bitmap-hll-file-format/)
17. [Laws and colleagues (2024). SPIDER: Improved Succinct Rank and Select Performance. SEA 2024, LIPIcs vol. 301.](https://drops.dagstuhl.de/entities/document/10.4230/LIPIcs.SEA.2024.21)

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

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Data structures › String and bitmap structures

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/bitmap

English original: https://www.edgepedia.org/bitmap
