#### Archived

This topic is now archived and is closed to further replies.

# " | " Operator and Flags

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

## Recommended Posts

Hi, I have a function like this, taking the following parameter:

void Clearbuffer(int buffer);

Now I use the function like this:

Clearbuffer(COLORBUFFER | ZBUFFER | STENCIL);

How do I find out wheter the variable _buffer contains all three flags or only one or two of them?

##### Share on other sites
Use bitwise AND: & and bitwise NOT: ~.
if (buffer & COLORBUFFER)    // have it, do something    ...if (!(buffer & ZBUFFER))    // no zbuffer here, do something    ...if (buffer & ~STENCIL)    // have something other than stencil    ...

##### Share on other sites

<pre>
if( buffer & (ZBUFFER | STENCIL) )
// both ZBUFFER and STENCIL

if( buffer == (ZBUFFER|STENCIL) )
// *only* ZBUFFER and STENCIL
</pre>

The difference between the two, of course, is that the first can have other flags set and still be true, whereas the second cannot.

##### Share on other sites
To elaborate, let''s say each of your bit flags is set up as such:
COLORBUFFER = 0001
ZBUFFER = 0010
STENCIL = 0100

If you call your function by using
Clearbuffer(COLORBUFFER | STENCIL)
int buffer = 0101 because (0001 | 0100) = 0101

Now, to find out if COLORBUFFER flag is set, you would do this:
(buffer & COLORBUFFER) because
(0101 & 0100) = 0100

if your buffer doesn''t have COLORBUFFER bit set (ie. 0001),
(buffer & COLORBUFFER) will get you 0000, because
(0001 & 0100) = 0000

Then you just send the results through some conditional statement (if...then, do...while, etc) and do whatever you gotta do. Hope that explains it.

##### Share on other sites
quote:
Original post by cruZ
Hi,

I have a function like this, taking the following parameter:

      void Clearbuffer(int buffer);

use unsigned values, they're easier to deal with, it wouldn't matter if you could specify binary values in C++ but since you can't it's easier to get them working with unsigned values

-----------------------------------------------------------
"People who usualy use the word pedantic usualy are pedantic!"-me

[edited by - Ecko on May 3, 2002 6:14:52 AM]

1. 1
2. 2
Rutin
25
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 22
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631764
• Total Posts
3002209
×