Flag: Difference between revisions
TomEdwards (talk | contribs) No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
A '''Flag''' is a [[boolean]] value that is stored in an [[integer]] variable. | A '''Flag''' is a [[boolean]] value that is stored in an [[integer]] variable. | ||
Up to 256 flags can be stored in a single int, since each is represented by one of its | Flags are stored together within an integer instead of a boolean for each flag because the smallest amount of memory that a program can access is a byte. If you used a boolean for the flags you'd be using a whole byte (8 bits) when all you need is 1 bit. An integer contains 256 bits. By placing the flags within an integer it means that the space is used much more efficiently than if you used a lots of booleans. Up to 256 flags can be stored in a single int, since each is represented by one of its bits. | ||
== Creation == | == Creation == |
Revision as of 12:52, 4 January 2009
A Flag is a boolean value that is stored in an integer variable.
Flags are stored together within an integer instead of a boolean for each flag because the smallest amount of memory that a program can access is a byte. If you used a boolean for the flags you'd be using a whole byte (8 bits) when all you need is 1 bit. An integer contains 256 bits. By placing the flags within an integer it means that the space is used much more efficiently than if you used a lots of booleans. Up to 256 flags can be stored in a single int, since each is represented by one of its bits.
Creation
Flags are created like this:
#define FL_MYFLAG 1<<0 #define FL_MYFLAG 1<<1 #define FL_MYFLAG 1<<2 etc.
Or sometimes like this (e.g. spawnflags):
#define FL_MYFLAG 1 #define FL_MYFLAG 16 #define FL_MYFLAG 32 etc.
You can then use the #defs in place of the numbers.
Usage
Flags are managed through functions, never directly. There are several different types of flags and each has its own set, but they all follow the same naming pattern:
Add<*>Flags()
Remove<*>Flags()
Clear<*>Flags()
Has<*>Flags()
(sometimesIs<*>FlagSet()
)
To operate on several flags at once you must use the bitwise operators &
, |
and ^
- the same as the normal boolean operators, but with only one character.
For instance:
if ( HasSpawnFlags(SF_CITIZEN_MEDIC | SF_CITIZEN_NOT_COMMANDABLE) ) ...