Sign in to follow this  
Slug

Witch bit?

Recommended Posts

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

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