为什么十六进制数以 0x 为前缀?
为什么十六进制数字前缀为0x
? 我了解前缀的用法,但不明白为什么选择 0x
的意义。
Why are hexadecimal numbers prefixed as 0x
?
I understand the usage of the prefix but I don't understand the significance of why 0x
was chosen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
注意:我不知道正确答案,但以下只是我个人的猜测!
正如前面提到的,数字前面的 0 表示它是八进制:
想象一下需要想出一个系统来表示十六进制数字,并注意我们正在 C 风格环境中工作。以类似 h 的汇编结尾怎么样?不幸的是你不能 - 它允许你创建有效标识符的标记(例如,你可以将变量命名为相同的东西),这会造成一些令人讨厌的歧义。
出于同样的原因,您不能以字符开头:
使用哈希可能会被丢弃,因为它与预处理器冲突:
最后,无论出于何种原因,他们决定在前导 0 后面放置一个 x 来表示十六进制。它是明确的,因为它仍然以数字字符开头,因此不能是有效的标识符,并且可能基于前导 0 的八进制约定。
Note: I don't know the correct answer, but the below is just my personal speculation!
As has been mentioned a 0 before a number means it's octal:
Imagine needing to come up with a system to denote hexadecimal numbers, and note we're working in a C style environment. How about ending with h like assembly? Unfortunately you can't - it would allow you to make tokens which are valid identifiers (eg. you could name a variable the same thing) which would make for some nasty ambiguities.
You can't lead with a character for the same reason:
Using a hash was probably thrown out because it conflicts with the preprocessor:
In the end, for whatever reason, they decided to put an x after a leading 0 to denote hexadecimal. It is unambiguous since it still starts with a number character so can't be a valid identifier, and is probably based off the octal convention of a leading 0.
它是一个前缀,指示数字是十六进制而不是其他基数。编程语言用它来告诉编译器。
示例:
0x6400
转换为6*16^3 + 4*16^2 + 0*16^1 +0*16^0 = 25600。
当编译器读取
0x6400
时,它在0x术语的帮助下理解该数字是十六进制的。通常我们可以理解为 (6400)16 或 (6400)8 或其他任何形式。对于二进制,它会是:
0b00000001
美好的一天!
It's a prefix to indicate the number is in hexadecimal rather than in some other base. The programming language uses it to tell compiler.
Example:
0x6400
translates to6*16^3 + 4*16^2 + 0*16^1 +0*16^0 = 25600.
When compiler reads
0x6400
, It understands the number is hexadecimal with the help of 0x term. Usually we can understand by (6400)16 or (6400)8 or whatever ..For binary it would be:
0b00000001
Good day!
前面的 0 用于表示以 2、8 或 16 为基数的数字。
在我看来,选择 0x 来表示十六进制,因为“x”听起来像十六进制。
只是我的意见,但我认为有道理。
再会!
The preceding 0 is used to indicate a number in base 2, 8, or 16.
In my opinion, 0x was chosen to indicate hex because 'x' sounds like hex.
Just my opinion, but I think it makes sense.
Good Day!
我不知道
0x
作为表示十六进制数字的前缀背后的历史原因 - 因为它肯定可以采取多种形式。这种特殊的前缀样式来自计算机科学的早期。由于我们习惯于十进制数字,因此通常不需要指示基数/基数。然而,出于编程目的,我们经常需要将基数与二进制 (base-2)、八进制 (base-8)、十进制 (base-10) 和十六进制 (base-16) 区分开来,这些是最常用的基数。
目前,它是用于表示数字基数的约定。我已经用上述所有基数及其前缀编写了数字 29:
0b11101
:二进制0o35
:八进制,用 o 表示0d29
:十进制,这是不寻常的,因为我们假设没有前缀的数字是十进制0x1D
:十六进制基本上,我们最常与基数相关联的字母表(例如 b 表示二进制)与
0< /code> 轻松区分数字的基数。
这特别有用,因为较小的数字可能会在所有基数中出现相同的混淆:0b1、0o1、0d1、0x1。
如果您使用的是富文本编辑器,则也可以使用下标来表示基数:12、18、110、1 <子>16
I don't know the historical reasons behind
0x
as a prefix to denote hexadecimal numbers - as it certainly could have taken many forms. This particular prefix style is from the early days of computer science.As we are used to decimal numbers there is usually no need to indicate the base/radix. However, for programming purposes we often need to distinguish the bases from binary (base-2), octal (base-8), decimal (base-10) and hexadecimal (base-16) - as the most commonly used number bases.
At this point in time it is a convention used to denote the base of a number. I've written the number 29 in all of the above bases with their prefixes:
0b11101
: Binary0o35
: Octal, denoted by an o0d29
: Decimal, this is unusual because we assume numbers without a prefix are decimal0x1D
: HexadecimalBasically, an alphabet we most commonly associate with a base (e.g. b for binary) is combined with
0
to easily distinguish a number's base.This is especially helpful because smaller numbers can confusingly appear the same in all the bases: 0b1, 0o1, 0d1, 0x1.
If you were using a rich text editor though, you could alternatively use subscript to denote bases: 12, 18, 110, 116
简短的故事:
0
告诉解析器它正在处理一个常量(而不是标识符/保留字)。仍然需要一些东西来指定数字基数:x
是任意选择。长话短说:在 60 年代,流行的编程数字系统是十进制和八进制 - 大型机每个字节有 12、18、24 或 36 位,可以很好地被 3 整除= log2(8)。
BCPL 语言对八进制数使用语法
8 1234
。当 Ken Thompson 从 BCPL 创建 B 时,他使用了0
前缀。这很棒,因为0
在两个基数中是相同的) ,00005 == 05
),并且#123
)。当从 B 创建 C 时,出现了对十六进制数的需求(PDP-11 有 16 位字和 8 位字节),并且上述所有要点仍然有效。由于其他机器仍然需要八进制,因此任意选择了
0x
(00
或0h
可能因尴尬而被排除)。C# 是 C 的后代,因此它继承了语法。
您可以在Dennis M. Ritchie 的页面找到有关 C 历史的详细信息。
Short story: The
0
tells the parser it's dealing with a constant (and not an identifier/reserved word). Something is still needed to specify the number base: thex
is an arbitrary choice.Long story: In the 60's, the prevalent programming number systems were decimal and octal — mainframes had 12, 18, 24 or 36 bits per byte, which is nicely divisible by 3 = log2(8).
The BCPL language used the syntax
8 1234
for octal numbers. When Ken Thompson created B from BCPL, he used the0
prefix instead. This is great because0
is the same in both bases),00005 == 05
), and#123
).When C was created from B, the need for hexadecimal numbers arose (the PDP-11 had 16-bit words and 8-bit bytes) and all of the points above were still valid. Since octals were still needed for other machines,
0x
was arbitrarily chosen (00
or0h
was probably ruled out as awkward).C# is a descendant of C, so it inherits the syntax.
You can find details about the history of C at Dennis M. Ritchie's page.