• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
lonewolff

Checking if a bit is set in a byte

24 posts in this topic

Hi Guys,

Just wondering if there is an easy way to check if a bit is set in a byte.

So, if I wanted to see it bit 5 was set for example?

Thanks in advance smile.png
0

Share this post


Link to post
Share on other sites

When you AND two values together, every bit that wasn't 1 in *both* values is set to zero. So if you want to check if a certain bit is set, you AND that one bit to a value, and then see if the result is 0 (the bit wasn't present), or if the result is the same what you AND'd.
 
if( (10110 & 10000) == 10000 )
if( (value & mask) == mask )
 
You can drop the " == mask " part because, in C++, non-zero integers automaticly convert to 'true':
if(value & mask) //If the result is 00000, then the statement evaluates to false, otherwise it evaluates to true.
 
 
Hodgeman's func does the same thing, but also uses an index to create the mask by shifting the value 00000001 over N places to become (for example) 00010000.


This actually seems to work very well. Easy to understand too smile.png
0

Share this post


Link to post
Share on other sites

if( (10110 & 10000) == 10000 )
if( (value & mask) == mask )


I wonder, why do some people write "== mask" instead of "!= 0"? I suppose it doesn't matter much when the variable is "mask" and/or you use a function, but I've seen this pattern with huge blocks of hard-coded if checks with duplicated giant enum names everywhere.
2

Share this post


Link to post
Share on other sites

 

 

if( (10110 & 10000) == 10000 )
if( (value & mask) == mask )


I wonder, why do some people write "== mask" instead of "!= 0"? I suppose it doesn't matter much when the variable is "mask" and/or you use a function, but I've seen this pattern with huge blocks of hard-coded if checks with duplicated giant enum names everywhere.

 

Consider what happens if mask contains more than one bit set. Comparing the AND-result against the mask checks if all bits are set, while comparing against 0 checks if any bit is set.

 

In that case it is not a matter of relative style or convention, but a matter of absolute correctness; one is correct and the other one is wrong.

 

 

To me it is also a question about clarity.  When you read the code, it is immediately clear what the intention is when you test against 'mask', rather than wondering for a few seconds why you are testing against 'not equal to zero'.

2

Share this post


Link to post
Share on other sites

I do it like this, but I'd have the enum in it's own header to be reused

enum struct BIT{
	_1 = 0x1,
	_2 = 0x2,
	_3 = 0x4,
	_4 = 0x8,
	_5 = 0x10,
	_6 = 0x20,
	_7 = 0x40,
	_8 = 0x80,
};

int eg = 20;

if(eg & BIT::_5){

}
0

Share this post


Link to post
Share on other sites

 

I do it like this, but I'd have the enum in it's own header to be reused

enum struct BIT{
	_1 = 0x1,
	_2 = 0x2,
	_3 = 0x4,
	_4 = 0x8,
	_5 = 0x10,
	_6 = 0x20,
	_7 = 0x40,
	_8 = 0x80,
};

int eg = 20;

if(eg & BIT::_5){

}

That forces people to be one-indexed for bit enumeration; sometimes, being zero-indexed is easier, when you use that index directly as the number of positions to shift. Or, put another way, if the index is the exponent of 2 for the value represented by the bit at that index.

2

Share this post


Link to post
Share on other sites
The strict does belong, it means you must access it with scope operator (BIT::) instead of making everything global.
0

Share this post


Link to post
Share on other sites
If you want to use the scope operator (which I endorse), it would be better to do:
 
struct BIT
{
    enum {
	_1 = 1 << 0,
	_2 = 1 << 1,
	_3 = 1 << 2,
	_4 = 1 << 3,
	_5 = 1 << 4,
	_6 = 1 << 5,
	_7 = 1 << 6,
	_8 = 1 << 7,
    };
};
0

Share this post


Link to post
Share on other sites
I'm not sure you can use scoped enums (enum class/enum struct) to do bitwise operations, since you can't convert them to int without an explicit cast. If you want to use it as a mask you need to stick a regular enum in a class or struct.
0

Share this post


Link to post
Share on other sites

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.
2

Share this post


Link to post
Share on other sites

 

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: [tt]0b'0010'1000[/tt], especially when multiple bits are set.

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

 

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.

0

Share this post


Link to post
Share on other sites

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".

Edited by Trienco
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0