• ### Announcements

#### Archived

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

# " | " Operator and Flags

## Recommended Posts

cruZ    133
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
IndirectX    122
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
Guest Anonymous Poster

<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
LyLFox    122
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
Ecko    122
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]