This topic is 4444 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I was wondering how people flags that were combinable were actually done. I was thinking about using them in my game, but i can only make them and they don't combine with each other. So I was wondering if someone could help me understand how that is done and how you read the DWORD or whatever you use and get the flags out from it. A few examples that use flags is the FVF Data Flags and the Windows style when creating a window. If someone could please show me a website that gives a good understanding of it or if they could please explain, that would be great. Thank you in advance.

##### Share on other sites
You're talking about bit flags? You'll have to understand something about binary...

12  6  3  18  4  2  6  8  4  2  1x  x  x  x  x  x  x  x

That is how [EDIT] char, or BYTE is laid out.

You can get exactly 8 unique flags out of a single BYTE, and the values correspond to 1,2,4,8,16,32,64, and 128.

As the below poster says, you can get more flags out of a DWORD...

Hexidecimal is your friend in that case:

#define  FLAG1              0x00000001#define  FLAG2              0x00000002#define  FLAG3              0x00000004...#define  FLAG32             0x80000000

If you need more, use an __int64... and just continue ...

#define FLAG1 1
#define FLAG2 2
#define FLAG3 4

etc...

You declare the flags variable like this:

DWORD flags = 0;

And you set the values using the or operator:

flags = FLAG1 | FLAG8;

If you wish to test whether a flag is set, do this:

if( FLAGNAME & flags ) // yep; your flag is set!

Is this what you're looking for?

[Edited by - Verg on April 23, 2006 12:31:03 AM]

##### Share on other sites
It seems to me that a DWORD is good for 32 flags.

##### Share on other sites
(crap) you're right... gonna hafta edit [grin]

##### Share on other sites
Quote:
 Original post by Verg#define FLAG1 1#define FLAG2 2#define FLAG3 4etc...

This would continue as:

#define FLAG4 8
#define FLAG5 16
#define FLAG6 32
...
#define FLAG32 2147483648

It's easier to define your flags in hex:

#define FLAG1 0x01
#define FLAG2 0x02
#define FLAG3 0x04
#define FLAG4 0x08
#define FLAG5 0x10
#define FLAG6 0x20
...
#define FLAG32 0x80000000

##### Share on other sites
Just to add... the calculator that comes with Windows is a nice tool for converting between binary/decimal/hex...

Start in binary mode

1 --> converts to 0x1
10 --> converts to 0x2
100 --> converts to 0x4

10000000000000000000000000000000 --> converts to 0x80000000

Just recognize that each flag in binary only sets a single "1" digit...

Hope I didn't confuse you up there :)

(Ironically, I think my mistake comes from looking at hex-editors too much... seeing 8 bytes in a row as a DWORD all the time... argh... it's late ;))

##### Share on other sites
I think I get it, so when windows does bitwise operators, it is in binary mode that it checks if that value and another value are present? If it is, i get it then, thanks.

##### Share on other sites
Quote:
 Original post by Bliz23I think I get it, so when windows does bitwise operators, it is in binary mode that it checks if that value and another value are present? If it is, i get it then, thanks.

Computers always work in binary mode.

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631419
• Total Posts
2999977
×