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

## Recommended Posts

How can i do to tell if a bit is in a bitfield? Any fast method?

##### Share on other sites
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 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 2int myFlag2 = 2; // 0010 base 2int myFlag3 = 4; // 0100 base 2int myFlag4 = 8; // 1000 base 2myBitField = 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   1010myFlag2      0010----------------             0010 > 0 therefore true
cheers,
-Danu

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

##### Share on other sites
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 on other sites
Quote:
 Original post by silvermaceint myBitField = 0;int myFlag1 = 1; // 001 base 2int myFlag2 = 2; // 010 base 2int myFlag3 = 3; // 011 base 2int myFlag4 = 4; // 100 base 2

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

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

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

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

##### Share on other sites
Quote:
 Original post by Anonymous PosterGiven: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 on other sites
Quote:
 Original post by Anonymous PosterYou 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 on other sites
wierd, in my engine its done right lol

##### Share on other sites
Quote:
 Original post by silvermacemonkey 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.

1. 1
2. 2
frob
20
3. 3
4. 4
Rutin
18
5. 5

• 30
• 13
• 10
• 9
• 18
• ### Forum Statistics

• Total Topics
632555
• Total Posts
3007059

×