Original Post
🔒 Locked
Bit flags
Advertisement
Bit flags are exactly what the name implyes. Flags that are one bit, e.g. on or off (true or false). A bit flag use a integer to hold the bit so that you can pass many options in a single variable. E.g you could deifne bitflags like this
Since it''s the bit''s that your chacking the difined vaule of the bit flag must always be twice the one before (e.g 1, 2, 4, 8, 16, 32 etc).
What it actuly does is setting the bits of a integer. An integer is nothing but a string of 1s and and 0s and by passing the vaules 1, 2, 4 etc using logical or what you do is setting the bits of those positions to 1. So if you were to pass the settings used in the pesudo code above you''d get a integer that looks like this (the intresting part) 10011 where the 1s tells you the option is one and 0 that it''s not.
#define USE_SYSTEMRAM 1
#define USE_DOUBLEBUFFER 2
#define FULLSCREEN 4
#define NO_ALPHA 8
#define NO_VSYNC 16
// Then lets say you got a function to set up a graphic interface
// Here''s the prototype
void Initialize(int options);
// You could now chose which options to use by passing the flags
// using the logical or operator ( | )
Initialize(USE_SYSTEMRAM | USE_DOUBLEBUFFER | NO_VSYNC);
// That would tell the function to initialize your gfx interface
// Using system ram, double buffer and not vsync
// Then inside Initialize(int options) you''d need to check for
// the optins. Someting like this
if(options & USE_SYSTENRAM) {
useSystemRam();
}
if(options & USE_DOUBLEBUFFER) {
useDoubleBuffer();
}
// Etc etc
Since it''s the bit''s that your chacking the difined vaule of the bit flag must always be twice the one before (e.g 1, 2, 4, 8, 16, 32 etc).
What it actuly does is setting the bits of a integer. An integer is nothing but a string of 1s and and 0s and by passing the vaules 1, 2, 4 etc using logical or what you do is setting the bits of those positions to 1. So if you were to pass the settings used in the pesudo code above you''d get a integer that looks like this (the intresting part) 10011 where the 1s tells you the option is one and 0 that it''s not.
typedef unsigned __int16 BITHOLDER;
BITHOLDER OPTION_1 = 1;
BITHOLDER OPTION_2 = 2;
BITHOLDER OPTION_3 = 4;
BITHOLDER OPTION_4 = 8;
// ...
BITHOLDER OPTION_16 = 32768;
void setoptions(BITHOLDER optFlags);
void main() {
BITHOLDER flagset;
flagset = flagset | OPTION_9 // SET option 9
flagset = flagset | OPTION_3 // SET option 3
flagset |= OPTION_14 // SET option 14
flagset = flagset XOR OPTION_9 // CLEAR/UNSET option 9
// What _is_ the exclusive-or operator in C++?
setoptions(flagset);
}
void setoptions(BITHOLDER optFlags) {
if (optFlags & OPTION_1) { /* ... */ }
if (optFlags & OPTION_2) { /* ... */ }
// ...
if (optFlags & OPTION_16) { /* ... */ }
}
I use them for tracking boolean flags, as well as for setting options and parameters. A single 16-bit integer will hold 16 BOOL values, whereas most compilers will turn a BOOL datatype into a 16-bit integer. BIG savings...
[edited by - Wyrframe on August 3, 2002 3:00:49 PM]
Bitwise Operations in C. That''s a really really good article that I''ve learned from. Here''s some common bitwise operations:
Turning a bit on:
lottabits |= bit;
Toggling a bit:
lottabits ^= bit;
Turning a bit off regardless of its prior state:
lottabits &= ~bit;
Testing a Bit value:
if (lottabits & bit)
Turning a bit on:
lottabits |= bit;
Toggling a bit:
lottabits ^= bit;
Turning a bit off regardless of its prior state:
lottabits &= ~bit;
Testing a Bit value:
if (lottabits & bit)
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement