Open In App

Binary literals in C++14 with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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




// C++ program to illustrate the
// Hexadecimal and Octal number
// using literals
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    // Hexadecimal number with
    // prefix '0x'
    int h = 0x13ac;
  
    // Octal number with prefix '0'
    int o = 0117;
  
    // Print the number of the
    // hexadecimal form
    cout << h << endl;
  
    // Print the number of the
    // octal form
    cout << o;
  
    return 0;
}


Output:

5036
79

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




// C++ program to illustrate the
// binary number using literals
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    // Binary literal with prefix '0b'
    int a = 0b00001111;
  
    cout << a << '\n';
  
    // Binary literal with prefix '0B'
    int b = 0B00001111;
    cout << b;
  
    return 0;
}


Output:

15
15


Last Updated : 28 Jan, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads