Binary literals in C++14 with Examples
Last Updated :
28 Jan, 2021
In this article, we will discuss Binary literals in C++14.
While writing programs which involve mathematical evaluations or various types of number, we usually like to specify each digit type with specific prefix i.e., For Hexadecimal number use the prefix ‘0x’ and for Octal number use the prefix ‘0’. Below is the program to illustrate the same:
Program 1:
C++14
#include <iostream>
using namespace std;
int main()
{
int h = 0x13ac;
int o = 0117;
cout << h << endl;
cout << o;
return 0;
}
|
Binary Literals: In the above way like in hexadecimal and octal numbers, now we can directly write binary literals (of the form 0’s and 1’s) in C++14. The binary number can be expressed as 0b or 0B as the prefix. Below is the program to illustrate the same:
Program 2:
C++14
#include <iostream>
using namespace std;
int main()
{
int a = 0b00001111;
cout << a << '\n' ;
int b = 0B00001111;
cout << b;
return 0;
}
|
Share your thoughts in the comments
Please Login to comment...