# 八进制

**八进制**是基数为8的位值制计数系统，使用0到7这八个数字，每一位的权值是8的幂。与十六进制一样，它主要作为二进制的一种紧凑简写而存在：因为8是2的幂，每个八进制数字恰好对应三个二进制位，因此可以逐位读出位串而无需进行算术运算<sup>[1](https://www.allaboutcircuits.com/textbook/digital/chpt-1/octal-and-hexadecimal-numeration/)</sup>。2024年《Parabola》杂志的一篇文章将二进制、四进制、八进制和十六进制归为“自然基数”，即基数为\( 2^{n} \)的计数系统，与十进制相对<sup>[2](https://www.parabola.unsw.edu.au/sites/default/files/2024-09/vol60_no2_10.pdf)</sup>。

| 关键事实 | 详情 |
|---|---|
| 数字与结构 | 基数为8的位值制计数系统，有效数字为0–7；一个八进制数字编码三个二进制位，因为\( 2^{3} = 8 \)<sup>[1](https://www.allaboutcircuits.com/textbook/digital/chpt-1/octal-and-hexadecimal-numeration/)</sup> |
| 参考值 | 644（八进制）= 420（十进制）；777 = 511<sup>[3](https://devdeck.dev/en/blog/octal-hex-conversion)</sup> |
| 历史契合 | 早期机器的字长可被三整除（12位PDP-8、18位PDP-7、24/36位IBM大型机），因此八进制数字与字长自然对齐<sup>[4](https://retrocomputing.stackexchange.com/questions/12101/what-is-the-hex-versus-octal-timeline)</sup><sup> • </sup><sup>[5](https://smartfact.blog/hexadecimal-octal-number-systems-guide)</sup> |
| 字节不匹配 | 一个8位字节需要2.67个八进制数字；首位数字只用了其三位容量中的一位，而两个十六进制数字恰好容纳一个字节<sup>[5](https://smartfact.blog/hexadecimal-octal-number-systems-guide)</sup> |
| 现存用途 | Unix文件权限（chmod 755、644）、航空应答机编码、Allen-Bradley PLC寻址<sup>[6](https://man.archlinux.org/man/chmod.1)</sup><sup> • </sup><sup>[7](https://electricalflux.com/learn-components/octal-binary-conversion-circuit-design)</sup> |
| 字面量语法 | 传统的前导零形式（0755）容易出错；显式的0o前缀（0o755）现已成为Python、Rust、PHP 8.1、ECMAScript 6等的标准<sup>[8](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p0085r3.html)</sup> |
| 近期标准动态 | 2024年1月，WG14以压倒性共识（18/1/1）同意将0o加入C2y，并以共识（11/4/4）将前导零八进制列为过时特性<sup>[9](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3319.htm)</sup> |

## 八进制与其他进制之间的转换

最快的转换路径经由二进制。从二进制小数点向两侧每三位一组划分，在两端补隐含的零以凑满最后一组，然后将每组翻译为对应的八进制数字。教科书中的例子将二进制10110111.1这样转换为八进制267.4<sup>[1](https://www.allaboutcircuits.com/textbook/digital/chpt-1/octal-and-hexadecimal-numeration/)</sup>。同样的方法反向也适用：二进制010011110分组为010 | 011 | 110，得到八进制0236<sup>[10](https://www.cprogramming.com/tutorial/base_systems.html)</sup>。

八进制转十六进制同样以二进制为桥梁：将每个八进制数字展开为三位，然后从右往左将位串重新划分为四位一组的半字节<sup>[3](https://devdeck.dev/en/blog/octal-hex-conversion)</sup>。有用的锚点：八进制777是十进制511（十六进制1FF），644是420（十六进制1A4）<sup>[3](https://devdeck.dev/en/blog/octal-hex-conversion)</sup>。

## 计算史上的八进制

八进制的早期主导地位源于字长。在IBM于1964年推出System/360（采用8位字节、32位字和24位地址）之前，字节、半字和字的尺寸往往是三的倍数，适合八进制；/360采用的位数是八的倍数，用十六进制表示恰好没有余位，因而最为合适<sup>[4](https://retrocomputing.stackexchange.com/questions/12101/what-is-the-hex-versus-octal-timeline)</sup>。影响深远的PDP-8的12位字恰好用四个八进制数字表示<sup>[5](https://smartfact.blog/hexadecimal-octal-number-systems-guide)</sup>，而IBM采用24位或36位字的大型机则是八进制的理想领地，每个字需要八个或十二个八进制数字<sup>[11](https://stackoverflow.com/questions/44569/octal-number-literals-when-why-ever)</sup>。

**DEC的八进制传统。** 数字设备公司（DEC）从1959年的PDP-1到1977年10月发布的VAX一直使用八进制。PDP-11的寄存器前面板专门按三位模式分组涂装，以便于前面板八进制编程<sup>[12](https://www.tuhs.org/pipermail/tuhs/2017-January/009623.html)</sup>。PDP-8的编程示例同样通篇使用八进制地址和操作码<sup>[13](https://www.landley.net/history/mirror/dec/pdp8faq.html)</sup>。

C语言继承了这个世界。它于1970年代初在18位DEC PDP-7上开发，其作者支持当时常用的进制——十进制、八进制和十六进制；二进制从未被加入C标准<sup>[14](https://embedded.fm/blog/embedded-wednesdays-octal)</sup>。更早的PDP机器具有12位地址空间，使指针恰好是四个3位八进制数字，C的八进制记法即源于此<sup>[15](https://stackoverflow.com/questions/1835465/where-did-the-octal-hex-notations-come-from)</sup>。

## 八进制在现代的用途

**Unix文件权限**是八进制最显著的现存角色。数字模式由一至四个八进制数字构成，通过将位值4、2、1相加得到；第一位数字选择setuid（4）、setgid（2）和粘滞位（1）属性，其余数字分别给出用户、组和其他人的读/写/执行权限<sup>[6](https://man.archlinux.org/man/chmod.1)</sup>。因此755意味着所有者拥有全部权限，组和其他人拥有读和执行权限<sup>[11](https://stackoverflow.com/questions/44569/octal-number-literals-when-why-ever)</sup>。三个八进制数字一眼就能对应所有者、组和其他人，不像十进制493或十六进制0x1ed那样难以辨认<sup>[11](https://stackoverflow.com/questions/44569/octal-number-literals-when-why-ever)</sup>。一个特殊之处：用数字模式清除目录上的setuid或setgid位需要额外的前导零，如00755<sup>[6](https://man.archlinux.org/man/chmod.1)</sup>。

**航空**领域依赖八进制，因为早期应答机只有6位用于身份识别，以两个八进制数字呈现，如“75”代表“111 101”；当身份识别长度扩展后，八进制惯例保留了下来<sup>[16](https://aviation.stackexchange.com/questions/37487/why-is-the-range-of-transponder-digits-0-to-7-rather-than-0-to-9)</sup>。模式3a应答机编码是一个12位数字，所有人都以四个八进制数字来处理<sup>[11](https://stackoverflow.com/questions/44569/octal-number-literals-when-why-ever)</sup>：飞行员输入从0000到7777的应答机编码（7700为紧急情况），硬件在模式A/C应答脉冲串中将这四个数字直接映射为12个二进制位<sup>[7](https://electricalflux.com/learn-components/octal-binary-conversion-circuit-design)</sup>。

**工业控制**在Allen-Bradley PLC寻址中保留了八进制：卡上的物理端子8在RSLogix 500中寻址为I:1/10<sup>[17](https://electricalflux.com/learn-guides/octal-system-definition-base-8-digital-electronics)</sup>。DMX512舞台灯光、电机驱动器和工业I/O模块上的DIP开关在物理上按三位一组布线，因此以八进制读取开关状态与丝印分组一致<sup>[7](https://electricalflux.com/learn-components/octal-binary-conversion-circuit-design)</sup>。

## 八进制与十六进制

比较归结为位对齐问题。一个八进制数字覆盖3位，一个十六进制数字覆盖4位；由于现代设备使用的位宽是八的倍数（8、16、32、64、128位），能被4整除但不能被3整除，十六进制成为更流行的简写<sup>[1](https://www.allaboutcircuits.com/textbook/digital/chpt-1/octal-and-hexadecimal-numeration/)</sup>。一个完整的8位字节需要三个八进制数字，其首位只用了容量中的一位，每字节约2.67个八进制数字，而十六进制恰好两个数字<sup>[5](https://smartfact.blog/hexadecimal-octal-number-systems-guide)</sup>。PDP-11展示了这种摩擦：尽管有16位寄存器和8位ASCII编码，它将数据按八进制数字分组，产生如177770这样的地址，其中高位数字只能是0或1<sup>[14](https://embedded.fm/blog/embedded-wednesdays-octal)</sup>。

这一转变发生在1970年代中后期，随着8080、6800和6502等8位字节架构占据主导，尽管在某些环境中，八进制直到1990年代仍是惯用进制<sup>[4](https://retrocomputing.stackexchange.com/questions/12101/what-is-the-hex-versus-octal-timeline)</sup>。在字段宽度为三位或非半字节对齐的场合，八进制仍保有实际优势：一个9位控制字如101 011 110恰好可表示为八进制536，而十六进制需要填充到12位<sup>[7](https://electricalflux.com/learn-components/octal-binary-conversion-circuit-design)</sup>。Unix权限掩码是每字段3位结构使八进制得以存续的典型例子<sup>[18](https://peps.python.org/pep-3127/)</sup>。

## 八进制字面量语法及近期变化

传统记法用前导零表示八进制，因此020是十进制16，而十六进制使用0x前缀<sup>[10](https://www.cprogramming.com/tutorial/base_systems.html)</sup>。前导零隐含地告诉词法分析器将该字面量按八进制处理，这对大多数用户来说是意想不到的，即使是高级用户也容易忽略<sup>[19](https://www.open-std.org/JTC1/SC22/WG14/www/docs/n3353.htm)</sup>。后果是具体的：`int timeout = 010;`等待8毫秒而非10毫秒<sup>[17](https://electricalflux.com/learn-guides/octal-system-definition-base-8-digital-electronics)</sup>。在PHP中，`16 === 016`求值为false，因为016解析为14<sup>[20](https://wiki.php.net/rfc/explicit_octal_notation)</sup>。

**0o前缀**是正在各语言中推广的修复方案。提供此特性的语言包括Haskell、OCaml、Python（从3.0版本起）、Raku、Ruby、Tcl（从9版本起）、PHP（从8.1版本起）、Rust，以及从ECMAScript 6开始的ECMAScript<sup>[8](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p0085r3.html)</sup>。Python 3.0完全移除了前导零八进制，只要前导“0”后面紧跟另一个数字就抛出SyntaxError，同时增加了0b二进制字面量<sup>[18](https://peps.python.org/pep-3127/)</sup>。JavaScript对已弃用的0前缀八进制字面量仅在严格模式下报错，并引导开发者使用0o；在严格模式下，非有效八进制字面量的前导零形式也被禁止，包括含有数字8或9或小数点的形式<sup>[21](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_octal_literal)</sup>。Python和Rust只接受显式的0o形式<sup>[20](https://wiki.php.net/rfc/explicit_octal_notation)</sup>。

**2023年以来的标准活动**进展迅速。在2024年1月的WG14会议上，向C2y添加0o/0O前缀获得了强烈共识（18票赞成、1票反对、1票弃权），将旧式前导零八进制字面量列为过时也获得了共识（11/4/4）<sup>[9](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3319.htm)</sup>。C提案N3353定义了现代语法，八进制字面量为前缀0o或0O后仅跟数字0到7，并弃用容易出错的前导零规则<sup>[19](https://www.open-std.org/JTC1/SC22/WG14/www/docs/n3353.htm)</sup>。C++论文P0085R3（2025年）提出了相同的修改，遵循N3353<sup>[8](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p0085r3.html)</sup>。MISRA C 2023与先前版本一样，在第7.1条（必需）下完全禁止八进制字面量<sup>[9](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3319.htm)</sup>。Carbon曾在早期提案中拒绝八进制字面量，后来批准添加0o前缀（例如0o755），主要是为了POSIX互操作性，如umask<sup>[22](https://docs.carbon-lang.dev/proposals/p006910-support-octal-literals.html)</sup>。

C委员会明确指出旧语法不能简单地改作他用：前导零永远不会改变含义而被接受为十进制字面量，因为它无法被安全地重新利用；它应保持为过时特性或被完全弃用并移除<sup>[9](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3319.htm)</sup>。

## References

1. [Octal and Hexadecimal Numeration, Electronics Textbook (All About Circuits)](https://www.allaboutcircuits.com/textbook/digital/chpt-1/octal-and-hexadecimal-numeration/)
2. [Opinion: Benefits of using base 8 over base 10 in society, Parabola Vol. 60 No. 2 (UNSW, 2024)](https://www.parabola.unsw.edu.au/sites/default/files/2024-09/vol60_no2_10.pdf)
3. [Octal to Hex Conversion: A Developer's Complete Guide, DevDeck](https://devdeck.dev/en/blog/octal-hex-conversion)
4. [What is the hex versus octal timeline?, Retrocomputing Stack Exchange](https://retrocomputing.stackexchange.com/questions/12101/what-is-the-hex-versus-octal-timeline)
5. [Hexadecimal and Octal Explained (Why Base 16 Dominates Computing), SmartFact](https://smartfact.blog/hexadecimal-octal-number-systems-guide)
6. [chmod(1), GNU coreutils manual](https://man.archlinux.org/man/chmod.1)
7. [Octal Binary Conversion: When to Use Base-8 vs Base-2 in Circuit Design, ElectricalFlux](https://electricalflux.com/learn-components/octal-binary-conversion-circuit-design)
8. [P0085R3: 0o octal literals for C++, ISO C++ committee paper (2025)](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p0085r3.html)
9. [N3319: octal-obsolete, WG14 C standard proposal](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3319.htm)
10. [Understanding Different Base Systems, Cprogramming.com](https://www.cprogramming.com/tutorial/base_systems.html)
11. [Octal number literals: When? Why? Ever?, Stack Overflow](https://stackoverflow.com/questions/44569/octal-number-literals-when-why-ever)
12. [TUHS mailing list: PDP-11, Unix, octal?](https://www.tuhs.org/pipermail/tuhs/2017-January/009623.html)
13. [PDP-8 Frequently Asked Questions](https://www.landley.net/history/mirror/dec/pdp8faq.html)
14. [0-1-2-3-4-5-6-7-10, Embedded.fm](https://embedded.fm/blog/embedded-wednesdays-octal)
15. [Where did the octal/hex notations come from?, Stack Overflow](https://stackoverflow.com/questions/1835465/where-did-the-octal-hex-notations-come-from)
16. [Why is the range of transponder digits 0 to 7 rather than 0 to 9?, Aviation Stack Exchange](https://aviation.stackexchange.com/questions/37487/why-is-the-range-of-transponder-digits-0-to-7-rather-than-0-to-9)
17. [Octal System Definition: When and Why to Use Base-8 in Digital Electronics, ElectricalFlux](https://electricalflux.com/learn-guides/octal-system-definition-base-8-digital-electronics)
18. [PEP 3127, Integer Literal Support and Syntax](https://peps.python.org/pep-3127/)
19. [N3353: Obsolete octal literals, ISO C committee paper](https://www.open-std.org/JTC1/SC22/WG14/www/docs/n3353.htm)
20. [PHP RFC: Explicit Octal Notation](https://wiki.php.net/rfc/explicit_octal_notation)
21. [SyntaxError: "0"-prefixed octal literals are deprecated, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Deprecated_octal_literal)
22. [Carbon Language proposal #6910: Support octal literals](https://docs.carbon-lang.dev/proposals/p006910-support-octal-literals.html)

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

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Arithmetic and number systems › Number systems › Integers and rational numbers

Copyright 2026 Edgepedia

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

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

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