# Binary representation similar to 0xFF ?

• 01-10-2003
DougDbug
Binary representation similar to 0xFF ?
Is there a binary representation similar to the way hex and octal numbers are represented?

I usually do this:
int MyMask = 0x08 ; // Hex mask format - OK

Is there something like this?
int MyMask = 0b1000 ; // Binary mask format - Wrong!

Im I correct that there is nothing like this in C++ ? I'm shocked that this doesn't seem to exist!

I don't have any problems converting between binary and hex, but I thought it might make the code more readable sometimes.

Ref: Primary compiler = MSVC++
• 01-10-2003
SilentStrike
There is no way to write binary literals in C++. They are long, that's probably why they aren't used.
• 01-10-2003
bennyandthejets
if you plan to make bit flags to find out if a certain bit is selected, declare an array of bit flags in your header file.

Code:

int bitFlag[8]={1,2,4,8,16,32,64,128};
That will enable you to get a bit status from a single byte variable. for use with shorts and longs, just extend the array to compensate. heres a function that i include in all my programs.

Code:

int GetBit(BYTE byte,int bit)
{
if ( (bit<0)||(bit>7) )
{
Notify(NULL,"bit request error; \nmust be between 0 and 7");
exit(ERR_GETBIT);
}

if (byte&bitFlag[bit])
return 1;

return 0;
}

dont steal my code, but learn from it if you need to. i doubt you'll need to, im probably not even talking about the same problem as you:D.
• 01-10-2003
DougDbug
THANKS
Thanks.

Yeah, SilentStrike, anything longer than a byte and you have to start counting bit-positions. But, I work with bytes a lot (hardware stuff).

I know I could write a function to do this, but I wanted to make the code more readable by making it easier to see which bits are being manipulated & tested. Sometimes I put the binary in a comment, or just make a comment like "If bit 3 is high". Adding a function (in this particular case) would make it more confusing. And other programmers would laugh at me saying "Why don't you just use hex?" :)

bennyandthejets: Good solution. There might me a time when something like this is better than using hex... (Hmmm....)