In code, I sometimes see people specify constants 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 know the C/C++ compilers won’t compile this, but there must be a workaround? Is it possible in other languages like Java?
I’d use a bit shift operator:
It is even better than flood of 0’s.