Advertisement

Passing Flags to a function...

Started by January 03, 2003 11:34 AM
9 comments, last by MattS423 21 years, 10 months ago
Hey, I read somewhere that when you check flags for a function, you use the
 &    
operator, because they might have done something like this
 function(FLAG_1 | FLAG_2);   
So...i decided to test it...and it doesn't work...take a look.
      
#include "stdafx.h"

#include "stdio.h"

#include "windows.h" //so DWORD is defined


//define flags


#define PF_FLAG0	1000

#define PF_FLAG1	1001

#define PF_FLAG2	1002

#define PF_FLAG3	1003

int PassFlags(DWORD Flags);

void main()
{

	PassFlags(PF_FLAG3);

}

int PassFlags(DWORD Flags)
{
	if(Flags & PF_FLAG0)
	{
		printf("Flag0\n");
	}
	if(Flags & PF_FLAG1)
	{
		printf("Flag1\n");
	}
	if(Flags & PF_FLAG2)
	{
		printf("Flag2\n");
	}
	if(Flags & PF_FLAG3)
	{
		printf("Flag3\n");
	}

	return 1;
}
    
no matter what flag i pass, it always says i have all of 'em. I know there is better way to do this, but how? EDIT: Gaa..spacing [edited by - MattS423 on January 3, 2003 12:36:52 PM]
Programmers of the world, UNTIE!
That is not that strange considering your "flags'" values: (I noticed after I wrote this that your values were in decimal and not hexadecmial, which is assumed below)

PF_FLAG0 = 1000 = %0001000000000000
PF_FLAG1 = 1001 = %0001000000000001
PF_FLAG2 = 1002 = %0001000000000010
PF_FLAG3 = 1003 = %0001000000000011

When you AND, this is what happens:
Flags = PF_FLAG3 = %0001000000000011   Flags    => %0001000000000011& PF_FLAG0 => %0001000000000000-------------------------------=             %0001000000000000 

The result, as you see, is > 0 and therefor true.
What you really want to do, is to let each flag represent their own bit. Like this:
#define PF_FLAG0    0x0001   // = %0000000000000001#define PF_FLAG1    0x0002   // = %0000000000000010#define PF_FLAG2    0x0004   // = %0000000000000100#define PF_FLAG3    0x0008   // = %0000000000001000#define PF_FLAG4    0x0010   // = %0000000000010000#define PF_FLAG5    0x0020   // = %0000000000100000#define PF_FLAGXX1  0x0800   // = %0000100000000000#define PF_FLAGXX2  0x2000   // = %0010000000000000 // This wayunsigned long flags;flags = PF_FLAG0 | PF_FLAG4 | PF_FLAGXX1;// is this://    0x0001 = %0000000000000001//    0x0010 = %0000000000010000// OR 0x0800 = %0000100000000000// -----------------------------// =  0x0811 = %0000100000010001 // When you want to check a flag, you do thisif(flags & PF_FLAG4)    std::cout << "PF_FLAG4 is set!\n";// Which calculate (flags & PF_FLAG4) like this://         flags = 0x0811 = %0000100000010001// AND  PF_FLAG4 = 0x0010 = %0000000000010000// ------------------------------------------// =    (result) = 0x0010 = %0000000000010000//// And 0x0010 is, surprise, true. 

This is really logical and easy, but it can take a while to "get it".



[edited by - CWizard on January 3, 2003 12:52:06 PM]
Advertisement
It also works when the flag''s value is a power of 2:

#include <iostream>#include <windows.h>#define FLAG1 2#define FLAG2 4#define FLAG3 8void flag_test(DWORD flag){  if(flag & FLAG1)  {    std::cout << "Flag1" << std::endl;  }  if(flag & FLAG2)  {    std::cout << "Flag2" << std::endl;  }  if(flag & FLAG3)  {    std::cout << "Flag3" << std::endl;  }}int main(){  flag_test(FLAG1|FLAG3);  return 0;} 
26.01.02 - the day we took revengeFC Schalke 04 - FC Bayern München 5:1 (2:0)
ok, that makes sense...i was thinking thats what would have to be done..

BUT! using a DWORD (or an unsigned long) i could have only 64 flags..am i correct? what if i need more than this? yeah, thats alot of flags, but wouldn''t a really big, fancy, compleicated MMOG (Star Wars Galaxies, for example) need more than 64 flags? I am aware that you can reuse some stuff...like this:

#define FUNCTION1_FLAG1 0x0001
...
#define FUNCTION2_FLAG1 0x0001

and that''s ok...but is there a case where you would need more than 64 flags for a particular function/gamestates/whatever?
Programmers of the world, UNTIE!
TheRealWanderer.... the hex values he showed are powers of 2. Just written in hex. which is a whole bunch easier than calculating them all.
AAAANNNDDD. MattS423, thats one way to do it, or have 2 flag variables, for example...
void Function( DWORD Flags1, DWORD Flags2 );
You useually call them different things though(like, Styles and ExStyles?
// Eric
DWORD = 32 bit, so you can only pass 32 flags. You can use QWORD for 64 flags, and if you need more flags, you have to use another way.

btw. nice function call would that be

function(FLAG1|FLAG2|FLAG3|FLAG4|
FLAG5|FLAG6|FLAG7|FLAG8...FLAG32);

My Site
Advertisement
oh, yeah...my bad...

That would be a nice function call...
Programmers of the world, UNTIE!
quote: Original post by erjo
TheRealWanderer.... the hex values he showed are powers of 2. Just written in hex. which is a whole bunch easier than calculating them all.


Uhm... yeah... yeah, that''s right. Hex values are not my strong point

26.01.02 - the day we took revengeFC Schalke 04 - FC Bayern München 5:1 (2:0)
Hmm, but he forgot one...
0x1
0x2
0x4
0x8
0xF <--- This one
0x10
0x20
0x40
0x80
0xF0 <--- AAANNNDDDD this one.
etc etc etc...

/ Eric
quote: Original post by erjo
Hmm, but he forgot one...
0x1
0x2
0x4
0x8
0xF <--- This one

Say what?
0xF = 0x1 | 0x2 | 0x4 | 0x8

HTTP 500 times 6


Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]

This topic is closed to new replies.

Advertisement