-
Flags, the ambiguity of
Hey guys, I just wanted see if i can get input on flags, and how to use them.
Basicly I've been seeing flags in apis(dx especially) And ive been curious exactly how flags work.
I understand generally you deliminate flags with | ( FLAG_ONE | FLAG_TWO ) .
The question is, in my function or where ever in my code, how do i find out what the flags are that were passed in?
Thanks for your time.
-
The bar does not delimit the flags. It applies a logical OR to the two values. Look up 'bit manipulation', 'bit shifting', etc...
int a, b = 1, c = 32;
a = b|c; //...combine
//...etc...
if( a & c ) //...test
a &= ~c; //...remove
//...etc, etc.
The general idea is to use a single variable to store many 'settings', thus saving space. That is, since, say, an unsigned integer has 32 bits (on most machines), we could conserve memory by placing boolean values into each bit position. Testing a certain bit tells us if that flag is set or not.
For instance, the answers to a true/false quiz of 96 questions could be stored on 3 unsigned ints, etc.