Making your own flags

Started by
6 comments, last by GameDev.net 18 years ago
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.
Advertisement
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 ...

Declare your flags as macros

#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?


Chad

[Edited by - Verg on April 23, 2006 12:31:03 AM]
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
It seems to me that a DWORD is good for 32 flags.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
(crap) you're right... gonna hafta edit [grin]
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
Quote:Original post by Verg
#define FLAG1 1
#define FLAG2 2
#define FLAG3 4

etc...


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
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 :)


Chad

(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 ;))
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
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.
Quote:Original post by Bliz23
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.


Computers always work in binary mode.

This topic is closed to new replies.

Advertisement