Witch bit?

Started by
9 comments, last by Slug 19 years, 5 months ago
How can i do to tell if a bit is in a bitfield? Any fast method?
Advertisement
Using bitmasks and bitwise operations
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
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]
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
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) );
}
Quote:Original post by silvermace
int 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


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


(Didn't sleep enough last night? ;-))
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)
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);}
monkey your right, /me edits.
wierd, in my engine its done right lol
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
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.

This topic is closed to new replies.

Advertisement