17
I realized that when it comes to hexadecimal numbers sometimes one is placed 0x
in front.
For example, 0xA1B2C3
instead of A1B2C3
.
What is that 0x
means?
17
I realized that when it comes to hexadecimal numbers sometimes one is placed 0x
in front.
For example, 0xA1B2C3
instead of A1B2C3
.
What is that 0x
means?
20
0x
or 0X
is a prefix that was initially used by AT&T Assembly compilers in the late 1960s to represent hexadecimal numerical values.
Bell Laboratories, at the time a subsidiary of AT&T, was the first to adopt the standard. It is also known for creating the operational environment UNIX, where it made wide use of this notation. Several syntactic descendants of *NIX (C, C#, Java, Javascript and others) propagated the use to the present day.
The 0
(initial zero) indicates that the value is a numerical constant; x
is phonetically similar in English to 'Hexa''.
+1 for including a history lesson in the answer :)
@Renan Thanks! I found it relevant, and it is always interesting to learn a little more about what you like =)
19
Has been answered by Lucas that "the prefix 0x identifies the number that follows as a hexadecimal constant", but I want to complement the answer with some relevant details:
P: Why use a prefix?
R: To differentiate from a decimal, because it is perfectly normal a hexa without any letter.0x99
, for example, it is153
decimally, but if you writeint x = 99
, the compiler could not guess his intention. o0x99
already makes explicit that is hexa.
Curiosities:
This is a real trick of the rascal: in some languages, the 0
(without the x
) before numbers, indicates "octal", thus, x = 032
is the same thing as x = 26
. This easily confuses anyone who has no experience with prefixes.
I’ve seen certain BASIC dialects using &b
or 0b
to indicate binary, for example 0b00001011
to represent the decimal 11
, as in some cases the &h
for hexadecimal (in MSX, &h8000
is the usual way to represent the beginning of the available memory for writing after a normal boot).
One of the languages I use a lot (Harbour), uses 0d
for dates. For example, dNascimento := 0d20010527
.
Is there a list of all the prefixes, or is it just these?
@Patrick the most common are the 0x
for hexadecimal and only 0
for octal, but it is an individual feature of the languages, not a standard. It is worth consulting the specific documentation of the same language.
Some languages realized that 0 for octal was not a good idea (it took, huh?) and are in the process of transitioning to remove this prefix or exchange it for 0o (with the letter "o" of "octal"). For example: Javascript, Python, Scala, D.
@(half-late) I didn’t understand the problem of using 0 for octal.
@Piovezan The first reason is in the answer: 032 (Oct) is 26 (Dec). Are you going to say that it’s not confusing that only a zero in the front changes the whole value of the number? That’s not how it should be... And if you are working with months (from 1 to 12) and want to put 0 forward to align, will give error in 08 and 09 (invalid octal numbers). Not to mention asymmetry: because hexadecimal uses 0x and octal is only 0? It must be from the time when octal was more used than hexadecimal.
8
The prefix 0x
identifies the number it follows as a hexadecimal constant, and the prefix 0
as a number in octal (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.
6
Some languages define 0x
as prefix to a hexadecimal number, it is basically a sign for the compiler/interpreter that the number should be treated on another basis(16).
Browser other questions tagged hexadecimal
You are not signed in. Login or sign up in order to post.
Languages use the prefix
0x
as a way of differentiating hexadecimal values from other values (int
,string
, etc.).– Kazzkiq