Jump to content
  • Advertisement
Sign in to follow this  
Slug

Witch bit?

This topic is 5052 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Guest Anonymous Poster
Using bitmasks and bitwise operations

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Given:

typedef enum
{
FLAG1 = 0,
FLAG2 = 1,
FLAG3 = 2,
FLAG4 = 4
} FLAGS;

int bitField = FLAG1 | FLAG3;

Then:

(bitField & FLAG1) will be non-zero
(bitField & FLAG4) will be zero

Share this post


Link to post
Share on other sites
simple enough, do you know about the bit-wise operators in your language of choice?

here's a quick C++ example:


int myBitField = 0;
int myFlag1 = 1; // 0001 base 2
int myFlag2 = 2; // 0010 base 2
int myFlag3 = 4; // 0100 base 2
int myFlag4 = 8; // 1000 base 2

myBitField = myFlag2 | myFlag4; // myBitFile => 1010 base 2

// test to see if a bit is on:
if( myBitField & myFlag2 ) {
std::cout << "Flag 2 is enabled." << std::endl;
}
the if statment does a simple and compare on the bits:
myBitField   1010
myFlag2 0010
----------------
0010 > 0 therefore true
cheers,
-Danu

[Edited by - silvermace on November 11, 2004 4:15:13 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can use something like this.

#define BIT(x) (1 << (x))

then use it for

bool testBit( int bitField, char bitNum )
{
return ( bitField & BIT(bitNum) );
}

Share this post


Link to post
Share on other sites
Quote:
Original post by silvermace

int myBitField = 0;
int myFlag1 = 1; // 001 base 2
int myFlag2 = 2; // 010 base 2
int myFlag3 = 3; // 011 base 2
int myFlag4 = 4; // 100 base 2



Um, last I checked, most bitfields had non overlapping bits. Aka:

int myBitField = 0;
const int myFlag1 = 1; // 0001
const int myFlag2 = 2; // 0010
const int myFlag3 = 4; // 0100
const int myFlag4 = 8; // 1000


Given your example,
myBitField = myFlag1 | myFlag2;
if (myBitField & myFlag3) //evaluates to true!!!


(Didn't sleep enough last night? ;-))

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Given:

typedef enum
{
FLAG1 = 0,
FLAG2 = 1,
FLAG3 = 2,
FLAG4 = 4
} FLAGS;

int bitField = FLAG1 | FLAG3;

Then:

(bitField & FLAG1) will be non-zero
(bitField & FLAG4) will be zero


Another person who didn't get enough sleep last night!!! The first statement will also be zero. Let's take a quick look at the binary values of the flags:

FLAG1 = 00000000 //Note: no 1 bits!!!
FLAG2 = 00000001
FLAG3 = 00000010
FLAG4 = 00000100

Now, some binary math:

00000000 (FLAG1)
| 00000010 (FLAG3)
--------------
= 00000010 (-> bitField)

Then the test:

00000010 (bitField)
& 00000000 (FLAG1)
--------------
= 00000000 (0, false)

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
You can use something like this.

#define BIT(x) (1 << (x))

then use it for

bool testBit( int bitField, char bitNum )
{
return ( bitField & BIT(bitNum) );
}


Why use a macro if you're just going to use it in a function?

bool testBit( int bitField , char bitNum )
{
return ( bitField & (1<<bitNum) );
}
void setBit( int & bitField , char bitNum )
{
bitField |= (1 << bitNum);
}
void clearBit( int & bitField , char bitNum )
{
bitField &= ~(1 << bitNum);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by silvermace
monkey your right, /me edits.
wierd, in my engine its done right lol


People's brains just tend to glaze over details sometimes ^_^. I've done similar. And the AP including a Flag1 = 0 just goes to show it's not just you XP.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!