I have noticed that when it comes to hexadecimal numbers, sometimes a "0x" is placed in the front.
Example:
0xA1B2C3
instead of
A1B2C3
What does this "0x" mean?
I have noticed that when it comes to hexadecimal numbers, sometimes a "0x" is placed in the front.
Example:
0xA1B2C3
instead of
A1B2C3
What does this "0x" mean?
It has already been answered by Lucas that "0x prefix identifies the number that follows as a hexadecimal constant", but I want to complement the answer with some relevant details:
P: To use a prefix?
A: To differentiate from a decimal, since hexa is perfectly normal with no letter at all.0x99
, for example, is153
in decimal, but if you typedint x = 99
, the compiler would not have guessed its intention. the0x99
already makes explicit that it is hexa.
Curiosities:
This is a real trickster trick: in some languages, 0
(without x
) before numbers, indicates "octal", so x = 032
is the same as x = 26
. This easily confuses those who have no experience with prefixes.
I have already seen certain BASIC dialects using &b
or 0b
to indicate binary, for example 0b00001011
to represent decimal 11
, as in some cases &h
to hexadecimal (in MSX , &h8000
is the usual way to represent the beginning of available memory for writing after a normal boot.)
One of the languages I use a lot (Harbor), uses 0d
for dates and 0t
for timestamp. 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 an AT & T subsidiary, was the first to adopt the standard. It is also known for creating the UNIX operating environment, where it made extensive use of this notation. Several * NIX syntactic descendants (C, C #, Java, JavaScript, and others) have propagated usage to this day.
The initial% (zero)% indicates that the value is a numeric constant; 0
is phonetically similar in English, with 'Hexa'.
The prefix 0x
identifies the number that follows 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;
}
It will print 16 and 8 on the screen.
Some languages define 0x
as a prefix of a hexadecimal number, basically it is a signaling to the compiler / interpreter that the number should be treated in another base (16).