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.
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.
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: the x is an arbitrary choice.
Long story: In the 1960s, 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 the 0 prefix instead. This is great because
0 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 octal numbers were still needed for other machines, 0x was arbitrarily chosen (00 or 0h 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.
0x over 00 was preference/awkwardness. 00 would break existing code. 0010 as octal is 8, while 0010 as hexidecimal would be 16. They couldn't use any number as a second digit indicator (except 8 or 9, and neither holds any significance related to hexidecimal) so a letter is a must. And that leaves either 0h or 0x (H e X idecimal). From this point it seems it's truly back to preference.0 prefix for octal has caused so very many problems over the years. Notably in countries like the UK where telephone numbers start with a 0. Javascript and many other languages would parse these as octal, mangling the number before storing. To add to the fun, one popular database product would silently switch back to decimal parsing if the number contained an 8 or 9.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:
04524 // octal, leading 0
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.
8000h // hex
FF00h // oops - valid identifier! Hex or a variable or type named FF00h?
You can't lead with a character for the same reason:
xFF00 // also valid identifier
Using a hash was probably thrown out because it conflicts with the preprocessor:
#define ...
#FF00 // invalid preprocessor token?
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.
0xFF00 // definitely not an identifier!
0xFFAB1234 must be written as 0FFAB1234h. I remember it from inline asm in Pascal when I was young stackoverflow.com/q/11733731/995714It's a prefix to indicate the number is in hexadecimal rather than in some other base. The programming language uses it to tell the compiler.
Example:
0x6400 translates to 6*16^3 + 4*16^2 + 0*16^1 +0*16^0 = 25600.
When the compiler reads 0x6400, it understands the number is hexadecimal with the help of the 0x term. Usually we can understand by (6400)16 or (6400)8 or whatever ..
For binary, it would be:
0b00000001
x6400? The x could still be used to infer hexadecimal.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!
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