What does "0x" mean at the beginning of hexadecimal numbers?
I realized that when it comes to hexadecimal numbers sometimes a 0x
is put in front.
For example, 0xA1B2C3
instead of A1B2C3
.
What does this 0x
mean?
4 answers
Has already been answered by Lucas that "the prefix 0x identifies the number that follows as a hexadecimal constant", but I want to supplement the answer with some relevant details:
Q: Why use a prefix?
A: to differentiate from a decimal, since it is perfectly normal a hexa with no letter at all.0x99
, for example, is153
in decimal, but if you wroteint x = 99
, the compiler would have no way of guessing your intent. o0x99
it already makes it explicit that it is hexa.
curiosities:
This is a real trick of the rogue: in some languages, the 0
(without the x
) before numbers, indicates "octal", thus, x = 032
is the same thing as x = 26
. This rather easily confuses those who do not have experience with prefixes.
I have seen certain basic dialects using &b
or 0b
to indicate binary, for example 0b00001011
to represent the decimal 11
, as well as in some cases the &h
for hexadecimal (no MSX, &h8000
it is the usual way to represent the start of memory available for writing after a normal boot).
One of the languages I use a lot (Harbour), uses 0d
for dates. For example, dNascimento := 0d20010527
.
0x
or 0X
is a prefix that was initially used by AT&T assembly compilers in the late 1960s to represent hexadecimal numeric values.
Bell Laboratories , at the time a subsidiary of AT&T, was the first to adopt the standard. She is also known for creating the operating environment UNIX, where she made extensive use of this notation. Various syntactic descendants of * NIX (C, C#, Java, JavaScript, and others) propagated usage up to the days current.
The initial 0
(zero) indicates that the value is a numeric constant; x
is phonetically similar in English to 'Hexa'.
The prefix 0x
identifies the following number as a hexadecimal constant, and the prefix 0
as an octal number (base 8).
For example,
#include <iostream>
using namespace std;
int main() {
cout << 0xF << endl;
cout << 010 << endl;
return 0;
}
Will print 15 and 8 on the screen.
Some languages define 0x
as a prefix of a hexadecimal number, basically it is a signal to the compiler/interpreter that the number should be treated on another basis(16).