Skip to content
Related Articles

Related Articles

Improve Article
Binary literals in C++14 with Examples
  • Difficulty Level : Medium
  • 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




// 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
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :