C++C++14C++17C++20Learn C++

Learn How To Use Binary Literals In Modern C++

Learn How To Use Binary Literals In Modern C++

C++14 brings us fewer but useful features to modern C++ than the standards preceding it. The creator of C++, Bjarne Stroustrup says that improvements in C++14 were deliberately lower in quantity compared to C++11. One of the small but useful features is the introduction of “Binary Literals”. C++14 comes with the Binary Literals feature, and in this post, we explain how we can use binary bits. 

Learn how to use binary literals in modern C++

binary literal is an integer literal that your binary number begins with 0b or 0B and consists of a sequence of binary digits (base two). It is defined in <bitset> and provides a convenient way to represent a binary base-2 number (1 and 0), useful for the developers in IDE, and useful to compilers that understand it is a binary value.

Here is a simple example of how we can use it.

Is there a simple example about how to use binary literals in modern C++?

We can use it with digital separators, which are another feature of C++. Here is an example, this time we used constants.

Note that, we can NOT use digital separator after the 0b literal as below.

Is there a full example about how to use binary literals in modern C++?

Here is a full example about how to use binary literals in C++.

For more details about the binary literals, please see https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3472.pdf

How To Allow Atomics Use In C++ Signal Handlers C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?

C++C++17Code SnippetLearn C++

How To Convert Char Array String To Unicode String Correctly In C++?

Artificial Intelligence TechC++Language FeatureLearn C++

How To Develop A Simple Hopfield Network In C++