Binary literals

0 votes

In code, I've seen constants specified in hex format like this:

const int has_nukes        = 0x0001;
const int has_bio_weapons  = 0x0002;
const int has_chem_weapons = 0x0004;
// ...
int arsenal = has_nukes | has_bio_weapons | has_chem_weapons; // all of them
if(arsenal &= has_bio_weapons){
  std::cout << "BIO!!"
}

But it doesn't make sense to me to use the hex format here. Is there a way to do it directly in binary? Something like this:

const int has_nukes        = 0b00000000000000000000000000000001;
const int has_bio_weapons  = 0b00000000000000000000000000000010;
const int has_chem_weapons = 0b00000000000000000000000000000100;
// ...

I'm aware that C/C++ compilers would not build this, but surely there must be a workaround? 

Is this doable in other languages, such as Java?


Jun 22, 2022 in C++ by Nicholas
• 7,760 points
601 views

1 answer to this question.

0 votes

Binary literals will be supported in C++14 with the following syntax:

0b010101010 /* more zeros and ones */

This functionality is already included in the most recent clang and gcc releases. 

You may test it by running those compilers with the -std=c++1y option.

answered Jun 27, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

Converting Binary to Decimal built-in function

In C++, I need to do a ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
735 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
686 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
2,384 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
1,448 views
0 votes
1 answer

Invalid method parameters for eth_sendTransaction

params needs to be an array, try {"jsonrpc":"2.0","method":"eth_se ...READ MORE

answered Sep 28, 2018 in Blockchain by digger
• 26,740 points
1,852 views
0 votes
1 answer

C++ binary operator overloading

The language defines several fundamental types, such as int and double.  Objects are instances of fundamental kinds. Let's pretend you have: struct Foo { ... }; You can use non-member functions to overload the operator+ function. Foo operator+(Foo , ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
570 views
0 votes
1 answer

C++ - Decimal to binary converting

The.to string() function of std::bitset returns a std::string containing a binary text representation with leading-zero padding. Use std::bitset32> to get 32-character strings from 32-bit integers, or std::bitset32> to get 32-character strings from 32-bit integers. #include <iostream> #include <bitset> int main() { ...READ MORE

answered Jun 10, 2022 in C++ by Damon
• 4,960 points
554 views
0 votes
1 answer

How do you express binary literals in Python?

For reference—future Python possibilities: Starting with Python 2.6 you ...READ MORE

answered Aug 14, 2018 in Python by aryya
• 7,460 points
1,046 views
+1 vote
1 answer

How do you express binary literals in Python?

Starting with Python 2.6 you can express ...READ MORE

answered Aug 28, 2018 in Python by Priyaj
• 58,020 points

edited Dec 21, 2023 by Khan Sarfaraz 4,322 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP