什么情况下使用八进制?
我经常看到二进制和十六进制使用,但从未见过八进制。然而,八进制在某些语言中使用时有其自己的约定(即,前导 0 表示八进制基数)。什么时候使用八进制?当人们使用八进制或八进制更容易推理时,有哪些典型情况?或者这只是一个品味问题?
I've seen binary and hex used quite often but never octal. Yet octal has it's own convention for being used in some languages (ie, a leading 0 indicating octal base). When is octal used? What are some typical situations when one would use octal or octal would be easier to reason about? Or is it merely a matter of taste?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
当一个字中的位数是 3 的倍数时,或者如果位的分组有意义以 3 为一组表示时,则使用八进制。示例是
在我大约 25 年的 IT 生涯中,除了 UNIX 文件权限位之外,我还没有遇到过任何八进制的使用。
但是,如果您的字中的位数是 4 的倍数,请务必使用十六进制。
Octal is used when the number of bits in one word is a multiple of 3, or if the grouping of the bits makes sense to notate in groups of 3. Examples are
I have not encountered any uses of octal other than unix file permission bits during my about 25 years in IT.
If the number of bits in your word is a multiple of 4, however, please do use hex, by all means.
八进制用作表示 UNIX 系统上文件权限的简写。例如,文件模式
rwxr-xr-x
将为0755
。Octal is used as a shorthand for representing file permissions on UNIX systems. For example, file mode
rwxr-xr-x
would be0755
.当语法是旧时代的遗留物时使用八进制,在某些平台上它可能有意义(系统字并不总是 8 位的倍数)。现在使用的是十六进制。
Octal is used when the syntax is a relic from the ages when it perhaps made sense on some platform (system words haven't always been a multiple of 8 bits). Nowadays hex is the thing to use.
八进制被更频繁使用的主要原因之一是,在你的头脑中在八进制和二进制之间转换比十六进制到二进制更容易:你只需要记住 8 个八进制数字(0-7)的二进制表示。
回到过去,调试意味着从一排 LED 读取寄存器内容,或者使用一组切换开关输入数据,这是一个大问题。许多早期计算机的面板将 LED 和开关分成三个一组,以方便实现这一点。
然而,十六进制开始胜出,因为字大小是 8 位字节的倍数开始胜出,并且读取和输入二进制数据的需要变得不必要(使用控制台文本 UI 和后来的 GUI 调试器)。
One of the main reasons octal used to be more frequently used was that it is easier to convert between octal and binary in your head than hex to binary: you only have to remember the binary representation of the 8 octal digits (0-7).
Back in the days when debugging meant reading register contents from a row of LEDs, or entering data with an array of toggle switches, this was a big concern. The panels on many of these early computers grouped the LEDs and switches in groups of threes to facilitate this.
However, hex began to win out as word sizes that are multiples of 8-bit bytes began to win out, and the need to read and enter data in binary became unecessary (with console text UI and later GUI debuggers).
没想到还有数字显示!
其他几个用途来自:
http://en.wikipedia.org/wiki/Octal
Didn't think of this but Digital displays!
Several other uses from:
http://en.wikipedia.org/wiki/Octal
如果鸟会数数,我猜它们会使用八进制。虽然大多数鸟类的羽毛“手”上有 3 个手指,但大多数是 Tetradactyly,意思是 4每只脚的脚趾。
If birds could count, my guess would be that they use octal. While most birds have 3 digits on their feathered "hands", most are Tetradactyly, meaning 4 toes on each foot.
在航空电子设备中,ARINC 429 字标签几乎总是以八进制表示。
In avionics, ARINC 429 word labels are almost always expressed in octal.
音乐,只要你远离(大多数)升号和降号。
Music, as long as you stay away from (most) sharps and flats.
仅供参考,有几个地方 Windows 和 javascript 自动确定以零为前缀的数字是八进制并转换该数字。
在 Windows 中,如果您 ping 和地址如 10.0.2.010,它实际上会 ping 10.0.2.8
如果您输入它作为计算机的 ip/dns 地址,Windows 也会执行此操作
虽然它已被弃用,但 Javascript 在某些函数(如 parseInt)上默认执行此操作如果不指定基数 http://www.w3schools.com/jsref/jsref_parseint.asp
FYI, there are a few places that windows and javascript automatically decide that a number prefixed with a zero is octal and convert the number.
In windows if you ping and address like 10.0.2.010 it will actually ping 10.0.2.8
Windows also does this if you enter it as the ip/dns address for the computer
Though it is deprecated, Javascript does this by default on some functions like parseInt if you do not specify a radix http://www.w3schools.com/jsref/jsref_parseint.asp