为什么用前导零表示八进制数字? [英] Why are leading zeroes used to represent octal numbers?
问题描述
我一直想知道为什么使用前导零(0o
.使用0o
会很有帮助,但不会引起领先于0
的问题(例如JavaScript中的parseInt('08');
).这种设计选择背后的原因是什么?
I've always wondered why leading zeroes (0
) are used to represent octal numbers, instead of — for example — 0o
. The use of 0o
would be just as helpful, but would not cause as many problems as leading 0
es (e.g. parseInt('08');
in JavaScript). What are the reason(s) behind this design choice?
推荐答案
所有现代语言都从C导入此约定,然后从B导入,然后从BCPL导入.
All modern languages import this convention from C, which imported it from B, which imported it from BCPL.
除了BCPL将#1234
用于八进制,将#x1234
用于十六进制. B偏离了此约定,因为#是B中的一元运算符(整数到浮点转换),因此无法使用#1234,并且#作为基本指示符被替换为0.
Except BCPL used #1234
for octal and #x1234
for hexadecimal. B has departed from this convention because # was an unary operator in B (integer to floating point conversion), so #1234 could not be used, and # as a base indicator was replaced with 0.
B的设计者试图使语法非常紧凑.我猜这是他们没有使用两个字符前缀的原因.
The designers of B tried to make the syntax very compact. I guess this is the reason they did not use a two-character prefix.
这篇关于为什么用前导零表示八进制数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!