关于C#:八进制数字:什么时候? 为什么? 曾经吗

关于C#:八进制数字:什么时候? 为什么? 曾经吗

Octal number literals: When? Why? Ever?

我从未在代码中使用过八进制数,也从未遇到过使用它的任何代码(尽管有十六进制和位旋转)。

我大约在1994年开始使用C / C ++进行编程,所以也许对此我还太年轻? 旧代码是否使用八进制? C通过在前面加上0来包括对这些字符的支持,但是使用这些基数为8的数字文字的代码在哪里?


最近,我不得不编写访问3位字段的网络协议代码。如果要调试,八进制会派上用场。

仅出于效果,您能告诉我这是3位字段吗?

1
0x492492

另一方面,这个相同的八进制数字:

1
022222222

现在,最后,以二进制形式(以3为一组):

1
010 010 010 010 010 010 010 010

这些天,我遇到的唯一八进制文字是在处理Linux中文件的权限位时,通常将其表示为3个八进制数字,其中每个数字分别代表文件所有者,组和其他用户的权限。

例如0755(大多数命令行工具也只有755)意味着文件所有者具有完全权限(读取,写入,执行),并且组和其他用户仅具有读取和执行权限。

以八进制表示这些位可以更轻松地确定设置了哪些权限。您可以一目了然地分辨出0755是什么意思,而不是493或0x1ed。


来自维基百科

At the time when octal originally
became widely used in computing,
systems such as the IBM mainframes
employed 24-bit (or 36-bit) words.
Octal was an ideal abbreviation of
binary for these machines because
eight (or twelve) digits could
concisely display an entire machine
word (each octal digit covering three
binary digits). It also cut costs by
allowing Nixie tubes, seven-segment
displays, and calculators to be used
for the operator consoles; where
binary displays were too complex to
use, decimal displays needed complex
hardware to convert radixes, and
hexadecimal displays needed to display
letters.

All modern computing
platforms, however, use 16-, 32-, or
64-bit words, with eight bits making
up a byte. On such systems three octal
digits would be required, with the
most significant octal digit
inelegantly representing only two
binary digits (and in a series the
same octal digit would represent one
binary digit from the next byte).
Hence hexadecimal is more commonly
used in programming languages today,
since a hexadecimal digit covers four
binary digits and all modern computing
platforms have machine words that are
evenly divisible by four. Some
platforms with a power-of-two word
size still have instruction subwords
that are more easily understood if
displayed in octal; this includes the
PDP-11. The modern-day ubiquitous x86
architecture belongs to this category
as well, but octal is almost never
used on this platform.

-亚当


I have never used octal numbers in my
code nor come across any code that
used it.

我敢打赌你有。根据标准,以零开头的数字文字是八进制的。包括0。每次您使用或看到字面的零时,它都是八进制的。奇怪但真实。 :-)


商用航空在古老的Arinc 429总线标准中使用八进制"标签"(基本上是消息类型ID)。因此,在为航空电子应用程序编写代码时,能够以八进制指定标签值非常好...


我还看到八进制在飞机应答器中使用。模式3a应答器代码是一个12位数字,每个人都将其视为4个八进制数字。有关Wikipedia的更多信息。我知道这通常与计算机无关,但FAA也使用计算机:)。


它对于Unix环境中的chmodmkdir函数很有用,但是除此之外,我无法想到其他任何常见用法。


没有任何世俗的理由来修改可以追溯到该语言诞生并且存在于众多程序中的标准。我仍然记得他们的ASCII字符
八进制值,则必须考虑得出A的十六进制值,但八进制为101;数字0是060 ... ^ C是003 ...

也就是说,我经常使用八进制表示。

现在,如果您真的想弯腰,请看一下PDP-10的字格式...


tar文件将信息存储为八进制整数值字符串


我是通过PDP-11与Octal接触的,因此,显然C语言是:)


学会使用PDP-8编程的任何人都对八进制数字怀有极大的热情。字长为12位,分为4组,每组3位,因此-1是8777八进制。 PDP-11保留了该方案,该方案具有16位字,但在各种事物上仍使用八进制表示,因此* NIX文件许可方案一直存在到今天。


从60年代和70年代末开始,仍然有许多旧的过程控制系统(Honeywell H4400,H45000等)被安排使用带有八进制寻址的24位字。考虑一下美国最后一批核电站的建造时间。

更换这些工业系统是一项相当艰巨的任务,因此,您可能很幸运地在野外遇到一个巨大的自定义浮点格式,然后在它们灭绝并惊叹之前,很幸运地遇到了一个!


在首个可用的显示硬件(7段显示器)上,八进制是最有用的。这些原始显示器没有以后可用的解码器。

因此,将数字寄存器输出分组以适合可用的显示,该显示只能显示八(8)个符号:0,1,2 3,4,5,6,7。

同样,第一批CRT显像管是光栅扫描显示器,最简单的字符符号生成器等效于7段显示器。

一如既往,激励人心的驱动程序是最便宜的显示器。


推荐阅读