# 位运算

位运算作用于一个或多个二进制数的各个位，而非这些位所表示的数值。C 标准定义了核心位运算符，在 C 中写作一元 `~` 以及二元 `<<`、`>>`、`&`、`^` 和 `|`，并要求其操作数为整数类型。<sup>[1](https://www.open-std.org/JTC1/SC22/WG14/issues/c23/issue1067.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 运算符集合 | C 位运算符为一元 `~` 与二元 `<<`、`>>`、`&`、`^`、`|`，作用于整数操作数<sup>[1](https://www.open-std.org/JTC1/SC22/WG14/issues/c23/issue1067.html)</sup> |
| 硬件成本 | 逻辑运算（与、或、异或、非、与非、或非、同或）与移位、循环移位运算一样，被视为单位成本的算术逻辑单元操作<sup>[2](https://iis-people.ee.ethz.ch/~zimmi/publications/comp_arith_notes_l.pdf)</sup> |
| 有符号移位 | 在 C 和 C++ 中，移位计数为负或至少等于操作数位宽属于未定义行为；C++20 将有符号右移定为算术右移（除以 2^b 并向负无穷取整）<sup>[3](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3793r2)</sup><sup> • </sup><sup>[4](https://en.cppreference.com/cpp/language/operator_arithmetic)</sup> |
| 最低置位 | `x & (x - 1)` 关闭最右侧的 1 位；`x & -x` 将其分离出来，因为补码取负的进位会穿过尾部零传播<sup>[5](https://www.informit.com/articles/article.aspx?p=1959565)</sup><sup> • </sup><sup>[6](https://dsa.handbook.academy/curriculum/bit-manipulation/bit-tricks-performance/)</sup> |
| 种群计数速度 | 硬件种群计数指令：Skylake 上延迟 3 周期、每周期一条；Zen 2 上延迟 1 周期、每周期 0.25 条；AVX2 计数达到每 8 字节 0.52 周期，是标量速率的两倍<sup>[6](https://dsa.handbook.academy/curriculum/bit-manipulation/bit-tricks-performance/)</sup><sup> • </sup><sup>[7](https://arxiv.org/pdf/1611.07612)</sup> |
| 2023 年以来的新进展 | RISC-V 位操作扩展 B 1.0.0 获批（2025 年 5 月）；英特尔 APX 将通用寄存器翻倍至 32 个<sup>[8](https://docs.riscv.org/reference/isa/v20250508/unpriv/b-st-ext.html)</sup><sup> • </sup><sup>[9](https://www.intel.com/content/www/us/en/developer/articles/technical/advanced-performance-extensions-apx.html)</sup> |
| 可移植性规则 | CERT INT13-C：仅在无符号整数操作数上使用位运算符，因为对有符号整数的结果由实现定义<sup>[10](https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/recommendations/integers-int/int13-c/)</sup> |

## 基本运算符：与、或、异或、非

每个二元位运算符都对其两个操作数的每个位位置独立应用一个布尔函数。两个一位输入恰好有 16 种可能的二元布尔函数，处理器可以通过选择等于该函数真值表值的四个操作码位，将它们全部实现为机器指令；与、或、异或及其否定（与非、或非、同或）是这一家族中为人熟知的成员。<sup>[11](https://www.informit.com/articles/article.aspx?p=1959565&seqNum=23)</sup> 苏黎世联邦理工学院的计算机算术讲义将与、或、异或、非、与非、或非和同或列为算术逻辑单元的算术逻辑操作，每种均为单位成本，在讲义的算术逻辑单元模型中被列为单位成本操作。<sup>[2](https://iis-people.ee.ethz.ch/~zimmi/publications/comp_arith_notes_l.pdf)</sup>

**补码行为。** 对有符号值的位运算作用于补码位模式。微软的 C 文档给出了 `-16 & 99` 的示例：在二进制表示上计算，结果为十进制 96。<sup>[12](https://learn.microsoft.com/en-us/cpp/c-language/c-bitwise-operators?view=msvc-170)</sup> 同样的模式无关性也是有符号算术本身的基础：在 4 位补码中，−2 + −3 的加法就是 1110 + 1101 的按位加法，丢弃最高位进位后得到 1011（−5）。<sup>[13](https://www.cs.huji.ac.il/course/2002/nand2tet/docs/ch_2_boolean_arithmetic.pdf)</sup> 在 C23 之前，负值可以使用反码或符号-幅值表示，这使得有符号类型上的位运算结果由实现定义，并可能使生成负零成为未定义行为；C23 强制要求补码表示，消除了这些问题。<sup>[1](https://www.open-std.org/JTC1/SC22/WG14/issues/c23/issue1067.html)</sup>

## 移位与循环移位

左移将位向最高有效端移动；右移将位向最低有效端移动。不同类型的区别在于空出的位置填充什么。逻辑移位用零填充。算术右移将符号位复制到高位，因此负值保持为负：在 C# 中，对 `int` 或 `long` 使用 `>>` 会传播符号位，而 `>>>` 始终执行逻辑移位，无论操作数类型如何都用零填充。<sup>[14](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators)</sup> 循环移位（circular shift）将从一个方向移出的位从另一侧移入；RISC-V 规范指出，循环移位取代了常见的四条指令序列：对计数取负、左移、右移、对结果进行或运算。<sup>[8](https://docs.riscv.org/reference/isa/v20250508/unpriv/b-st-ext.html)</sup>

对于无符号操作数，C++ 将 `a << b` 定义为 a × 2<sup>b</sup> 对 2<sup>N</sup> 取模，其中 N 是返回类型的位宽；对于负的 `a`，`a << b` 是未定义行为。<sup>[4](https://en.cppreference.com/cpp/language/operator_arithmetic)</sup> 对于负的 `a`，`a >> b` 在 C++20 之前由实现定义；自 C++20 起为算术右移，等于 a/2<sup>b</sup> 向负无穷取整。<sup>[4](https://en.cppreference.com/cpp/language/operator_arithmetic)</sup>

**超范围移位计数**是最尖锐的可移植性隐患。在 C 和 C++ 中，如果移位计数为负或不小于左操作数的位数，行为是未定义的。<sup>[4](https://en.cppreference.com/cpp/language/operator_arithmetic)</sup> x86 处理器表现出回绕行为，将移位量对位宽取模，因此相同的源代码在其他目标上可能静默地做不同的事。<sup>[3](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3793r2)</sup> 语言选择差异很大：C#、Java 和 JavaScript 将移位量对位宽取模；Fortran、Objective-C、Perl 和 Ruby 向另一方向移位；OCaml 和 Rust 给出未指定或实现定义的结果。<sup>[3](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3793r2)</sup> Carbon 语言则在语言层面将超范围移位视为编程错误，要求第二个操作数介于零（含）和位宽（不含）之间，并在加固构建中产生 0 或 −1 而非未定义行为。<sup>[15](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/expressions/bitwise.md)</sup>

语言设计在存在哪些移位类型上也存在差异。提供显式逻辑移位与算术移位选择的语言通常缺少无符号类型（Fortran、Java、JavaScript、OCaml）；C# 在版本 11 之前缺少逻辑右移运算符；Go、Haskell、Python、Ruby 和 Rust 对负操作数始终执行算术移位。<sup>[3](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3793r2)</sup> Java 和 C# 为两种右移设有单独的运算符，`>>` 符号扩展，`>>>` 零填充。<sup>[6](https://dsa.handbook.academy/curriculum/bit-manipulation/bit-tricks-performance/)</sup> Carbon 明确拒绝添加单独的算术与逻辑移位运算符以及循环移位运算符，并记录道：在无溢出的情况下，左移是乘以 2 的幂，右移是除以 2 的幂并向下取整。<sup>[15](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/expressions/bitwise.md)</sup>

## 用数字说话

种群计数（POPCNT）指令返回 16、32 或 64 位操作数中置位的数量，若操作数为零则设置 ZF，并清除其他算术标志；AMD 在 K10 中引入，英特尔在 Nehalem 中引入。<sup>[16](https://x86-64.net/isa/popcnt/)</sup> 在近期的英特尔处理器上，它每周期执行一次，延迟 3 周期；在 Zen 2 上延迟 1 周期，吞吐量每周期 0.25 条。<sup>[7](https://arxiv.org/pdf/1611.07612)</sup><sup> • </sup><sup>[6](https://dsa.handbook.academy/curriculum/bit-manipulation/bit-tricks-performance/)</sup> 阿格纳·福格（Agner Fog）的指令表是跨 x86 微架构此类延迟和倒数吞吐量的标准参考，其中 ADD 的倒数吞吐量为 0.33 意味着每周期三次整数加法。<sup>[17](https://www.agner.org/optimize/instruction%5Ftables.pdf)</sup>

单指令多数据改变了批量位计数的经济性。基于 AVX2 的种群计数在大型数组上达到每 8 字节 0.52 周期，是优化的基于 popcnt 的函数（每 8 字节 1.02 周期）速度的两倍，而 AVX2 Harley-Seal 基数计算比基于 popcnt 的方法快 2.4 倍。<sup>[7](https://arxiv.org/pdf/1611.07612)</sup> 2024 年一项关于位置种群计数的研究发现，AVX-512 实现在 512 KiB 输入时峰值达到 91.0 GB/s，比克拉奎斯特（Klarqvist）等人的 1024 位内核（59.4 GB/s）提升 53%，AVX2 实现峰值为 34.8 GB/s。<sup>[18](https://arxiv.org/abs/2412.16370)</sup> AVX-512 的 `vpternlogd` 根据立即数真值表计算任意三输入一输出的位运算，例如奇偶校验用 0x96，多数表决用 0xe8；相应的 `_mm*_ternarylogic*` 内建函数对三个源操作数并发执行两个位逻辑运算，SSE 或 AVX2 中没有等价物，AVX-512 还提供 64 位掩码内建函数，如 `_kand_mask64`、`_kxor_mask64` 和 `_kshiftri_mask64`。<sup>[18](https://arxiv.org/abs/2412.16370)</sup><sup> • </sup><sup>[19](https://builders.intel.com/docs/networkbuilders/intel-avx-512-instruction-set-for-packet-processing-technology-guide-1617440657.pdf)</sup> 更宽的向量并不线性扩展：在 Skylake 上，512 位 AVX-512 向量每周期最多执行两条指令，而 256 位 AVX2 每周期三条，因此向量长度翻倍仅带来 33% 的性能提升。<sup>[18](https://arxiv.org/abs/2412.16370)</sup>

## 位操作技巧

**清除与分离最低置位。** 公式 `x & (x - 1)` 关闭字中最右侧的 1 位，若没有则产生 0；例如 01011000 变为 01010000。<sup>[5](https://www.informit.com/articles/article.aspx?p=1959565)</sup> 该结果基于忽略溢出的补码（或无符号）算术，如 ACM 发表的关于字并行逻辑与加法指令的分析所示。<sup>[20](https://dl.acm.org/doi/pdf/10.1145/359605.359632)</sup> 配对表达式 `x & -x` 提取最低置位：在补码中，对 x 取负将所有位反转并加一，该增量产生的进位穿过 x 的尾部零——在最低有效 1 处反转后停止。<sup>[6](https://dsa.handbook.academy/curriculum/bit-manipulation/bit-tricks-performance/)</sup>

**计数位。** Kernighan 循环对每个置位应用一次 `n &= n - 1`，因此运行 popcount(n) 次迭代而非 WORD_BITS 次，在稀疏输入上最多以 WORD_BITS / popcount(n) 的比例胜出。<sup>[6](https://dsa.handbook.academy/curriculum/bit-manipulation/bit-tricks-performance/)</sup> 在支持硬件种群计数的目标上，标准库种群计数函数如 `std::popcount`、`Integer.bitCount`、`bits.OnesCount32` 和 Python 3.10+ 的 `int.bit_count()` 可能降为硬件种群计数指令；性能因处理器而异。<sup>[6](https://dsa.handbook.academy/curriculum/bit-manipulation/bit-tricks-performance/)</sup> 没有该指令时，计数位需要一系列移位、掩码和加法。<sup>[16](https://x86-64.net/isa/popcnt/)</sup>

**已证明的极限。** 《黑客的乐趣》（*Hacker's Delight*）的作者亨利·S·沃伦（Henry S. Warren）证明，没有任何右传播风格的指令序列能够关闭最左侧的 1 位、执行可变左移或循环移位、或计数尾部零，因为此类操作需要检查所关注位置左侧的位。<sup>[5](https://www.informit.com/articles/article.aspx?p=1959565)</sup> 肖恩·E·安德森（Sean E. Anderson）的《位操作技巧》补充了一个实用警告：CPU 和指令集的细微差别意味着基准测试是确定哪种位技巧变体在给定机器上更快的最佳方法。<sup>[21](https://graphics.stanford.edu/~seander/bithacks.html)</sup>

## 掩码、标志与应用

掩码按运算符选择字中的位字段：用或时，选中的位被置一；用与和反转掩码时，它们被清除；单用与时，它们被测试或提取；用异或时，它们被翻转。<sup>[22](https://www.embedonix.com/articles/definitive-guide-bit-manipulation-embedded-systems/)</sup> 嵌入式实践中，掩码常量保持为无符号，并注意其位宽，例如对 32 位寄存器使用 `UINT32_C(1) << bit`，并使用目标位操作指令，如 Cortex-M CMSIS 的 `__RBIT`、`__REV`、`__CLZ` 和 `__ROR`，或 GCC 内建函数如 `__builtin_bswap32`、`__builtin_clz` 和 `__builtin_popcount`。<sup>[22](https://www.embedonix.com/articles/definitive-guide-bit-manipulation-embedded-systems/)</sup>

**位集**每个元素打包一位，使集合运算成为字并行位指令。它们在 C++ `std::bitset` 中的布局由实现定义，因此内存假设必须按工具链和 ABI 验证。<sup>[23](https://swarnimarun.com/posts/bitsets-in-practice-cache-masks-and-simd)</sup> 基准测试表明收益取决于密度：AVX-512 扫描密集位集耗时 166.93 ns，而逐位迭代为 242.52 ns，加速 1.45 倍；而在稀疏-后段位集上，基于计数尾部零的扫描比逐位迭代快约 74 倍（25,498 ns 对 1,855,886 ns）。<sup>[23](https://swarnimarun.com/posts/bitsets-in-practice-cache-masks-and-simd)</sup>

**异或在完整性与密码学中的应用。** 异或是自身的逆运算，在循环冗余校验算术中加法和减法都等价于它，将基本运算简化为单个自逆运算。<sup>[24](https://www.zlib.net/crc_v3.txt)</sup> 循环冗余校验将比特流视为多项式的系数，就像十进制数字是基 10 的系数一样，通过移位、掩码和异或计算，例如 `c = (c << 1) ^ 0x07`。<sup>[25](https://www.usb.org/sites/default/files/crcdes.pdf)</sup><sup> • </sup><sup>[26](https://cmagic.dev/lessons/checksums)</sup> 最简单的异或校验和，即块校验字符 `d[0] ^ d[1] ^ ... ^ d[n-1]`，使每个位位置成为该列在所有字节上的奇偶校验；它能检测单比特故障，但可能漏掉某些多比特错误，并且由于异或可交换，字节重排不会改变它，因此字节被打乱的帧也能通过校验。<sup>[26](https://cmagic.dev/lessons/checksums)</sup> RFC 1071 将互联网校验和规定为 16 位字的反码和，并指出仅对数据做异或是一种极其微弱的校验。<sup>[27](https://www.rfc-editor.org/rfc/rfc1071.html)</sup> 除校验和外，异或还出现在奇偶校验、流密码、布隆过滤器、Roaring 位图和位图数据库索引中，AES、ChaCha20 和 SHA 等密码学原语也由异或、循环移位和移位构建。<sup>[28](https://go-tools.org/blog/bitwise-operations-complete-guide)</sup> RISC-V Zbkb 扩展现已冻结，打包了实现密码学工作负载中常见操作所必需的位操作指令。<sup>[29](https://github.com/riscv/riscv-bitmanip/blob/main/bitmanip/zbkb.adoc)</sup>

## 位运算符与逻辑运算符，以及可移植性陷阱

在 C 中，逻辑与运算符类型为 `int`，仅当两个操作数都比较不等于零时值为 1，逻辑与和逻辑或使用短路求值：左操作数之后有一个序列点，如果它已经确定结果，则右操作数完全不被求值。<sup>[30](https://en.cppreference.com/c/language/operator_logical)</sup> C 的设计者有意做出区分：使用逻辑与控制逻辑流，仅在执行位级操作时使用按位与。<sup>[31](https://www.cs.princeton.edu/courses/archive/spring18/cos217/lectures/05_DesignOfC.pdf)</sup> 混淆它们会产生两类错误：`x & 1` 与 `x && 1` 改变值语义，而用 `&` 替换 `&&` 会静默移除短路，因此受保护的右侧（例如解引用前的空检查）无论如何都会被求值。

**有符号操作数**是第二个陷阱。CERT 规则 INT13-C 指出，位运算符应仅与无符号整数操作数一起使用，作为可移植性建议，特别是因为某些有符号操作（如 C 中负值的右移）具有实现定义的行为；有符号负值的右移可能实现为算术或逻辑移位，而位移位在某些情况下可能是未定义行为。在最坏的情况下，对有符号数执行位运算可能导致缓冲区溢出和攻击者执行任意代码。<sup>[10](https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/recommendations/integers-int/int13-c/)</sup> 安德森的位技巧参考补充说，1989 年 ANSI C 规范未规定有符号右移的结果，因此依赖它的技巧不可移植，而为负值产生全一比特的右移需要带符号操作数，而容易溢出的减法应转换为无符号。<sup>[21](https://graphics.stanford.edu/~seander/bithacks.html)</sup> 此后情况已收窄：C++20 将有符号右移定义为算术移位，C23 强制要求补码。<sup>[4](https://en.cppreference.com/cpp/language/operator_arithmetic)</sup><sup> • </sup><sup>[1](https://www.open-std.org/JTC1/SC22/WG14/issues/c23/issue1067.html)</sup>

**高级语言模拟。** JavaScript 位运算符将操作数强制转换为 32 位有符号整数，因此 `1 << 31` 为 −2147483648，移位计数对 32 取模，所以 `1 << 32` 等于 1，`>>>` 提供无符号右移，−8 >>> 1 得到 2147483644。<sup>[28](https://go-tools.org/blog/bitwise-operations-complete-guide)</sup> 由于 Python 缺少固定宽度整数类型，它不提供 `>>>` 运算符；`~5` 求值为 −6，因为那里的补码推理无限向左延伸，而在 JavaScript 中，像 `1n << 40n` 这样的 BigInt 字面量为位运算提供任意精度。<sup>[28](https://go-tools.org/blog/bitwise-operations-complete-guide)</sup>

## 近期进展

**RISC-V。** 位操作扩展 B 版本 1.0.0 已在 2025 年 5 月的非特权 ISA 规范中获批，包含 Zba、Zbb、Zbc、Zbs 和 Zbkb 子扩展。<sup>[8](https://docs.riscv.org/reference/isa/v20250508/unpriv/b-st-ext.html)</sup>

**x86。** 英特尔 APX 将通用寄存器数量从 16 个翻倍至 32 个，通过新的 REX2 前缀在传统整数指令集中访问；为 APX 编译的代码包含的加载少 10%，存储少 20% 以上，指令少 10%，无需修改源代码重新编译即可实现收益。<sup>[9](https://www.intel.com/content/www/us/en/developer/articles/technical/advanced-performance-extensions-apx.html)</sup> APX 还增加了加载、存储和比较/测试指令的条件形式，以及抑制常见指令状态标志写入的选项，扩展了条件转换。<sup>[9](https://www.intel.com/content/www/us/en/developer/articles/technical/advanced-performance-extensions-apx.html)</sup>

**软件。** 标准库种群计数现已成为各语言中实现该技巧的惯用写法：C++ 中的 `std::popcount`、Java 中的 `Integer.bitCount`、Go 中的 `bits.OnesCount32` 以及 Python 3.10+（2021 年 10 月）中的 `int.bit_count()` 在现代 x86 和 ARM 上均降为单条硬件指令。<sup>[6](https://dsa.handbook.academy/curriculum/bit-manipulation/bit-tricks-performance/)</sup> 在研究方面，穆拉（Mula）和勒米尔（Lemire）的基于 AVX2 混洗的算法在 32 字节向量上计数位，吞吐量为标量种群计数指令的两倍，包括 glibc 的 memrchr、jq、DuckDB 和 ClickHouse 在内的库链接到 libpopcnt。<sup>[6](https://dsa.handbook.academy/curriculum/bit-manipulation/bit-tricks-performance/)</sup> 2024 年的位置种群计数工作将其扩展到 AVX-512 和 ASIMD 内核。<sup>[18](https://arxiv.org/abs/2412.16370)</sup>

## References

1. [C issue 1067: Bitwise operators on signed types, WG14](https://www.open-std.org/JTC1/SC22/WG14/issues/c23/issue1067.html)
2. [Computer Arithmetic: Principles, Architectures, and VLSI Design, ETH Zürich](https://iis-people.ee.ethz.ch/~zimmi/publications/comp_arith_notes_l.pdf)
3. [Better shifting (P3793R2), C++ committee paper](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2026/p3793r2)
4. [Arithmetic operators, cppreference.com (C++)](https://en.cppreference.com/cpp/language/operator_arithmetic)
5. [Hacker's Delight: 2-1 Manipulating Rightmost Bits, Henry S. Warren](https://www.informit.com/articles/article.aspx?p=1959565)
6. [Bit tricks for performance, The DSA Handbook](https://dsa.handbook.academy/curriculum/bit-manipulation/bit-tricks-performance/)
7. [Faster Population Counts Using AVX2 Instructions (arXiv)](https://arxiv.org/pdf/1611.07612)
8. ["B" Extension for Bit Manipulation, Version 1.0.0, RISC-V Ratified Specifications](https://docs.riscv.org/reference/isa/v20250508/unpriv/b-st-ext.html)
9. [Introducing Intel Advanced Performance Extensions (Intel APX)](https://www.intel.com/content/www/us/en/developer/articles/technical/advanced-performance-extensions-apx.html)
10. [INT13-C. Use bitwise operators only on unsigned operands, CERT Secure Coding](https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/recommendations/integers-int/int13-c/)
11. [2-23 Implementing Instructions for All 16 Binary Boolean Operations, Hacker's Delight](https://www.informit.com/articles/article.aspx?p=1959565&seqNum=23)
12. [C Bitwise Operators, Microsoft Learn](https://learn.microsoft.com/en-us/cpp/c-language/c-bitwise-operators?view=msvc-170)
13. [2. Boolean Arithmetic, Nand to Tetris](https://www.cs.huji.ac.il/course/2002/nand2tet/docs/ch_2_boolean_arithmetic.pdf)
14. [Bitwise and shift operators, C# reference, Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators)
15. [Carbon language design docs: bitwise expressions](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/expressions/bitwise.md)
16. [POPCNT instruction set extension, x86-64.net](https://x86-64.net/isa/popcnt/)
17. [Instruction Tables, Agner Fog](https://www.agner.org/optimize/instruction%5Ftables.pdf)
18. [Faster Positional-Population Counts for AVX2, AVX-512, and ASIMD (arXiv, 2024)](https://arxiv.org/abs/2412.16370)
19. [Intel AVX-512 Instruction Set for Packet Processing Technology Guide](https://builders.intel.com/docs/networkbuilders/intel-avx-512-instruction-set-for-packet-processing-technology-guide-1617440657.pdf)
20. [Functions realizable with word-parallel logical and two's-complement addition instructions, ACM](https://dl.acm.org/doi/pdf/10.1145/359605.359632)
21. [Bit Twiddling Hacks, Sean E. Anderson](https://graphics.stanford.edu/~seander/bithacks.html)
22. [The Definitive Guide to Bit Manipulation for Embedded Systems, EMBEDONIX](https://www.embedonix.com/articles/definitive-guide-bit-manipulation-embedded-systems/)
23. [Bitsets in Practice: Cache Lines, Masks, and SIMD Tradeoffs](https://swarnimarun.com/posts/bitsets-in-practice-cache-masks-and-simd)
24. [A Painless Guide to CRC Error Detection Algorithms](https://www.zlib.net/crc_v3.txt)
25. [Cyclic Redundancy Checks in USB, USB-IF](https://www.usb.org/sites/default/files/crcdes.pdf)
26. [Checksums — integrity over the wire, cmagic.dev](https://cmagic.dev/lessons/checksums)
27. [RFC 1071: Computing the Internet checksum](https://www.rfc-editor.org/rfc/rfc1071.html)
28. [Bitwise Operations Explained: AND, OR, XOR, Shifts, and Masks, Go Tools](https://go-tools.org/blog/bitwise-operations-complete-guide)
29. [riscv-bitmanip zbkb.adoc](https://github.com/riscv/riscv-bitmanip/blob/main/bitmanip/zbkb.adoc)
30. [Logical operators, cppreference.com (C)](https://en.cppreference.com/c/language/operator_logical)
31. [The Design of C: A Rational Reconstruction, Part 2, Princeton COS 217](https://www.cs.princeton.edu/courses/archive/spring18/cos217/lectures/05_DesignOfC.pdf)

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

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Arithmetic and number systems › Computational arithmetic › Hardware arithmetic units

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/bitwise-operation

English original: https://www.edgepedia.org/bitwise-operation
