Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "0b" and "0x" stand for when assigning binary and hex?

Tags:

c++

hex

binary

When assigning a binary value and a hexadecimal value directly you can do it as follows (respectively):

uint8_t val1 = 0b10101;
uint8_t val2 = 0xFF;

What does the 0b and 0x mean? Specifically the 0 at the front. Can you have other values instead of 0?

Also as another curious question, what other characters can go in the place of b and x? Is there one for octal as an example?

like image 781
Ruan Avatar asked Aug 22 '19 08:08

Ruan


People also ask

What is 0b and 0x?

Binary is a base-2 numbering system (digits are either 0 or 1), while hexadecimal is a base-16 numbering system (digits 0-F). Look online for descriptions of how these systems work.

What does 0b mean in hex?

Thus, we sometimes prefix binary numbers with "0b" (zero b) to differentiate binary numbers from numbers in base 10 representation (so instead of using 0100, we would say that 4 in decimal is equivalent to 0b0100 in binary).

What does 0x mean in hex?

The prefix 0x is used in code to indicate that the number is being written in hex. But what is 'B' doing in there? The hexadecimal format has a base of 16, which means that each digit can represent up to 16 different values.

What does 0x mean in binary?

Save this answer. Show activity on this post. 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.


3 Answers

What does the 0b and 0x mean?

They mean that the nuneric literal is respectively in binary and hexadecimal base.

Can you have other values instead of 0?

A numeric literal starting with a non zero digit will be a decimal literal.

Also as another curious question, what other characters can go in the place of "b" and "x"?

Besides b and x, any octal digit can go there in which case it is the most significant digit of an octal literal.

like image 189
eerorika Avatar answered Sep 24 '22 19:09

eerorika


Any and all integer literals you can create are summarized in the C++ standard by the grammar production at [lex.icon]

integer-literal:
    binary-literal integer-suffixopt
    octal-literal integer-suffixopt
    decimal-literal integer-suffixopt
    hexadecimal-literal integer-suffixopt

binary-literal:
    0b binary-digit
    0B binary-digit
    binary-literal 'opt binary-digit

octal-literal:
    0
    octal-literal 'opt octal-digit

decimal-literal:
    nonzero-digit
    decimal-literal 'opt digit

hexadecimal-literal:
    hexadecimal-prefix hexadecimal-digit-sequence

binary-digit:
    0
    1

octal-digit: one of
    0  1  2  3  4  5  6  7

nonzero-digit: one of
    1  2  3  4  5  6  7  8  9

hexadecimal-prefix: one of
    0x  0X

hexadecimal-digit-sequence:
    hexadecimal-digit
    hexadecimal-digit-sequence 'opt hexadecimal-digit

hexadecimal-digit: one of
    0  1  2  3  4  5  6  7  8  9
    a  b  c  d  e  f
    A  B  C  D  E  F

As we can deduce from the grammar, there are four types of integer literals:

  • Plain decimal, that must begin with a non-zero digit.
  • Octal, any number with a leading 0 (including a plain 0).
  • Binary, requiring the prefix 0b or 0B.
  • Hexadecimal, requiring the prefix 0x or 0X.

The leading 0 for octal numbers can be thought of as the "O" in "Octal". The other prefixes use a leading zero to mark the beginning of a number that should not be interpreted as decimal. "B" is intuitively for "binary", while "X" is for "hexadecimal".

like image 34
StoryTeller - Unslander Monica Avatar answered Sep 23 '22 19:09

StoryTeller - Unslander Monica


0b (or 0B) denotes a binary literal. C++ has allowed it since C++14. (It's not part of the C standard yet although some compilers allow it as an extension.) 0x (or 0X) is for hexadecimal.

0 can be used to denote an octal literal. (Interestingly 0 itself is an octal literal). Furthermore you use the escape sequence \ followed by digits to be read in octal: this applies only when defining const char[] literals using "" or char or multicharacter literals using ''. The '\0' notation that you often see to denote NUL when working with strings exploits that.

In the absence of a user defined literal suffix, any numeric literal starting with a non-zero is in denary.

There are rumblings in the C++ world to use 0o for an octal literal and perhaps even drop support for the leading zero version. Although that would be an hideous breaking change.

like image 29
Bathsheba Avatar answered Sep 22 '22 19:09

Bathsheba