Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are hexadecimal numbers prefixed with 0x?

Tags:

c

syntax

hex

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.

like image 781
unj2 Avatar asked Apr 19 '10 20:04

unj2


People also ask

Is hexadecimal 0x or ox?

Hexadecimal is the base-16 number system, as opposed to the base 2 system (binary) and base 10 system (decimal). The prefix we use for hexadecimal is "0x". To represent the numbers 0-9, we simply use those digits.

What does 0x mean in programming?

In C and languages based on the C syntax, the prefix 0x means hexadecimal (base 16). Thus, 0x400 = 4×(162) + 0×(161) + 0×(160) = 4×((24)2) = 22 × 28 = 210 = 1024, or one binary K.

Where does 0x come from?

0x means the number is probably hexadecimal. This applies in C/C++, and probalby other languages.

Which numbering system uses a 0x notation?

In programming, a number of notations are used to denote hexadecimal numbers, usually involving a prefix. The prefix 0x is used in C, which would denote this value as 0xDB3E .


2 Answers

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 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 the 0 prefix instead. This is great because

  1. an integer constant now always consists of a single token,
  2. the parser can still tell right away it's got a constant,
  3. the parser can immediately tell the base (0 is the same in both bases),
  4. it's mathematically sane (00005 == 05), and
  5. no precious special characters are needed (as in #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.

like image 198
Řrřola Avatar answered Nov 03 '22 00:11

Řrřola


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! 
like image 28
AshleysBrain Avatar answered Nov 03 '22 00:11

AshleysBrain