Sign in to follow this  
Bliz23

Making your own flags

Recommended Posts

Bliz23    188
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 this post


Link to post
Share on other sites
Verg    450
You're talking about bit flags? You'll have to understand something about binary...



1
2 6 3 1
8 4 2 6 8 4 2 1

x 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]

Share this post


Link to post
Share on other sites
bakery2k1    712
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

Share this post


Link to post
Share on other sites
Verg    450
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 ;))

Share this post


Link to post
Share on other sites
Bliz23    188
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this