Cpp - Binary Literals and digit separator

Binary Literals

Integer literals now can be expressed as binary values and can contain digit separators.

To express an integer in binary, precede it with the characters "0b" or "0B", as in this statement:

int role = 0b11111111; 

This gives role the decimal value 255.

Here's another example that sets role to decimal 15:

int role = 0b00001111; 

Binary literals can be used in expressions as well, such as this statement:

int role = 0b00001111 - 0b00001101; 

The result of the expression is decimal 2 (the binary literal 0b00000010).

Digit Separator

A digit separator is a character placed in large numbers to make them more readable.

The digit separator in C++ is the single quote character ('). Here's a statement that uses it:

long long ouch = 19'241'111'111'222; 

The separator character (') is ignored by the C++ compiler.

Related Topic