Checking if a bit is set in a byte

Started by
23 comments, last by Trienco 9 years, 10 months ago

The whole BIT enum idea is practically pointless once you get binary literals:


int flags = 0b000001 | 0b010000;
 
std::cout << "Bit 1 is " << ((flags & 0b000001)? "set" : "not set") << ".\n";
std::cout << "Bit 2 is " << ((flags & 0b000010)? "set" : "not set") << ".\n";
std::cout << "Bit 3 is " << ((flags & 0b000100)? "set" : "not set") << ".\n";
std::cout << "Bit 4 is " << ((flags & 0b001000)? "set" : "not set") << ".\n";
std::cout << "Bit 5 is " << ((flags & 0b010000)? "set" : "not set") << ".\n";
std::cout << "Bit 6 is " << ((flags & 0b100000)? "set" : "not set") << ".\n";

[Ideone runnable example]

GCC already has binary literals as an extension, and binary literals are being added in C++14.

Advertisement

The whole BIT enum idea is practically pointless once you get binary literals:


You're still going to want to give them names, right? I don't want to have put in a comment explaining what bit "0b001000" is when I could have used something with a name instead. And writing "0b001000" doesn't look any better than writing (1 << 3) to me.

Binary literals will be more useful for multiple bits, I suspect.

The whole BIT enum idea is practically pointless once you get binary literals:


You're still going to want to give them names, right?

Descriptive names for actual flags and masks? Absolutely! But for names like BIT_ONE, BIT_TWO? Probably not. I don't have enums for decimal literals: ONE = 1, TWO = 2, and I don't really see too much of a need for enumerating each bit in an integer, unless the names actually carry meaning.

I don't want to have put in a comment explaining what bit "0b001000" is when I could have used something with a name instead. And writing "0b001000" doesn't look any better than writing (1 << 3) to me.


Aye, that's true: 1 << 3 is relatively compact and clear.

Perhaps digit separators can make it (somewhat) easier to read though: 0b'0010'1000, especially when multiple bits are set.

(digit separators work with other numeral literals, like floats: 0.123'456'789, or integers: 1'200'350)

Unfortunately, unlike binary literals (already available in GCC), I don't think any compiler has digit separators implemented, since they only finally settled on the symbol to use just last month (it'll possibly make it into C++14 with binary literals) - I wish they would've gone with a space instead, but there were too many problems.

You're correct that in actual code, I'd use a descriptively named constant. Magic numbers, regardless of base, are likely to bite later in a project. laugh.png

I'm just glad binary literals are finally in the standard.

Interestingly enough, most people I know who deal with bit fiddling on a daily base don't even really care about binary literals. Using hex numbers is second nature by now and how many people are really going to specify anything beyond 8 bits in a ridiculously long binary representation? In fact, if there are any literals that REALLY need a separator, it's binary ones (it can't get any longer and harder to read than that). I didn't even know those exist and never really missed or needed them, but the first thing about binary literals was "please let me group them, so I won't have to painfully count if it's bit 29 or 30".

f@dzhttp://festini.device-zero.de

This topic is closed to new replies.

Advertisement