This wiki is out of date, use the continuation of this wiki instead

Bit flags

From FenixWiki

Revision as of 12:35, 8 March 2008 by Sandman (Talk | contribs)
Jump to: navigation, search

Definition

Bit flags are constants which each denotes a single unique case in one situation and can be combined to form different, unique cases. They are called bit flags, because when bits are used to denote a cases, we observe they are indeed single and unique and can be combined to form different unique combinations.

Bit flags are often used as integers:

Bit pattern - Integer value
0001 - 1
0010 - 2
0100 - 4
1000 - 8

These can be combined to form, for example:

Bit pattern - Integer value
1001 - 9
0110 - 6
1110 - 14
0101 - 5

Example

When we look at blit flags for example, we see the values:

Constant - Value - Description
B_HMIRROR - 1 - Blit the graph horizontally mirrored.
B_VMIRROR - 2 - Blit the graph vertically mirrored.
B_TRANSLUCENT - 4 - Blit the graph with half transparency.
B_ALPHA - 8 - Blit the graph in some way. (What does this do exactly?)
B_ABLEND - 16 - Blit the graph using additive blending (nice effect for fire).
B_SBLEND - 32 - Blit the graph using subtractive blending (nice effect for ghosting).
B_NOCOLORKEY - 128 - Blit the transparent parts of the graph as black.

These are all single unique cases and can be combined to form different unique cases. For example, when we want a translucent, horizontally mirrored blit operation with use of additive blending, we would do:

B_HMIRROR | B_TRANSLUCENT | B_ABLEND
1         | 4             | 16       = 21

Because the bits are unique, the addition operator can also be used. But when we consider there would be a constant called B_HVMIRROR, which has the value B_HMIRROR|B_VMIRROR (3), the addition operator can't be used all the time:

B_HMIRROR | B_HVMIRROR == 1 | 3 == 3
B_HMIRROR + B_HVMIRROR == 1 + 3 == 4

In conclusion, use the bor operator when dealing with bit flags to be on the safe side.

Personal tools