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


Setting bitflags

Recommended Posts

I am curious as to how to set bitflags. I want to set bitflags in an 8-bit char. Like, how would I be be able to change a certain bit in the char? I want to use bitflags to create a flag system in my game. For example, taking a treasure chest would set the 2nd bit in the variable, then with that flag set, the player can''t open the same treasure chest twice. If any of you know how to do this, please let me know. Thank you very much.

Share this post

Link to post
Share on other sites
OR, AND and XOR operators will come in handy.

unsigned char flag1 = 1<<0;
unsigned char flag2 = 1<<1;
unsigned char flag3 = 1<<2;
unsigned char flag4 = 1<<3;

unsigned char bitter = 0;
bitter |= flag2; //set flag 2
bitter |= flag4; //set flag 4
if (bitter & flag2) {} //is flag2 set? yes
if (bitter & flag3) {} //is flag3 set? no

bitter ^= flag2; //toggle flag2. since it was on, it''ll be turned off
Learn more from google by typing ''boolean operators tutorial'' or something similar.

Share this post

Link to post
Share on other sites

const unsigned char BITFLAG_1 = 0x01;
const unsigned char BITFLAG_2 = 0x02;
const unsigned char BITFLAG_3 = 0x04;
const unsigned char BITFLAG_4 = 0x08;
const unsigned char BITFLAG_5 = 0x10;
const unsigned char BITFLAG_6 = 0x20;
const unsigned char BITFLAG_7 = 0x40;
const unsigned char BITFLAG_8 = 0x80;
// those are hexadecimal values, for each bit

unsigned char YourFlags = 0;

// to set bit 2:
YourFlags |= BITFLAG_2;

// to unset bit 3:
YourFlags &= (~BITFLAG_3);

// check if bit 6 is set:
if(YourFlags & BITFLAG_6)

Share this post

Link to post
Share on other sites
You'd probably be better off looking up "bit-wise" operators. "Boolean operators" will probably get you "&&", "||", or "!" instead of "&", "|", "~", "^"

[edited by - GrinningGator on August 5, 2003 6:49:06 PM]

Share this post

Link to post
Share on other sites
Its done using bitwise operators.... and the ''flag'' or ''mask'' is what you use to get the specific value out. Now before I continue I must say that this method of storing data can save a GREAT deal of memory (use only 1 bit per bool value rather than a whole 8bit byte). But it takes many more calculations as you have to get the values out. The difficult part is that you can''t write values in binary (I don''t know how to anyway), so this is how I do it with an 8 bit data object (a char, or bool).

Binary Hex
00000000 0x00
00000001 0x10
00000010 0x20
00000100 0x40
00001000 0x80
00010000 0x10
00100000 0x20
01000000 0x40
10000000 0x80

Now, I''m not a big fan of hexadecimal and never have been (too lazy to learn it really) so I can''t explain exactly why that works but that is how I handle my return value objects. Now to the useful part, how to get the data.... Here is an example

if(Value & 0x10) //00000001
//The value of the bit is 1
//The value of the bit is 0

Here is how that if statment works
Value(in binary) Return value
00000000 0/false
00000001 1/true
00000010 0/false
01000000 0/false
00010000 0/false
00000000 0/false
00100001 1/true

I''m sure I''ve typed alot more than needed.... if you would like more or even the code for my return value object (uses two 4byte data objects to allow for 1024 possible unque return values (8bits*4bytes) * (8bits * 4Bytes).

Pat - Ex nihilo nihilus

Share this post

Link to post
Share on other sites
Patindahat, you really need to reread your post... For one, your binary hex table is crap (hmm...two 0x10, wth), and it seems you based the rest of your post on that. Ummm, yeah...

The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ

Share this post

Link to post
Share on other sites