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.
相关问题
- Multiple sockets for clients to connect to
- What does an “empty line with semicolon” mean in C
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
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.
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!
It's a prefix to indicate the number is in hexadecimal rather than in some other base. The C 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 reads0x6400
, 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
Hope I have helped in some way.
Good day!
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, 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 all of the points above were still valid. Since octals were still needed for other machines,
0x
was arbitrarily chosen (00
was probably ruled out as awkward).C# is a descendant of C, so it inherits the syntax.