[c++] Binary literals

In C++, you can represent binary numbers directly by using the 0b prefix. This feature was introduced in C++14.

Syntax

The syntax for binary literals is as follows:

0b10101010

In the above example, 0b denotes the beginning of a binary literal, followed by the sequence of binary digits representing the number.

Example

Here’s a simple example to demonstrate the usage of binary literals:

#include <iostream>

int main() {
    int binaryNumber = 0b10101010;
    std::cout << "Binary number: " << binaryNumber << std::endl;
    return 0;
}

When you run the above program, it will output:

Binary number: 170

In this example, 0b10101010 is a binary literal representing the decimal value 170.

By using binary literals, you can directly specify binary values in your C++ code, making it easier to work with binary data.

Conclusion

Binary literals provide a convenient way to work with binary numbers in C++, making the code more readable and expressive.

For more information, you can refer to the C++ Standard.