Jump to content
  • Advertisement
Sign in to follow this  
Yamian

function multiple enum inputes

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

when using an enumeration in a function, how can you make it so it has multipe inputes with |, like in SDL_init(SDL_Init_Video|SDL_Init_Audio)?

Share this post


Link to post
Share on other sites
Advertisement
Make the numeric values correspond to an individual bit, you can test for that bit being set, and hence for that flag inside the function.

Share this post


Link to post
Share on other sites
Well, for example

int flag1 = 1; //01
int flag2 = 2; //10
int flag3 = 4; //100

int flags = flag1 | flag2 | flag3; //111

void func(int flags){
if(flags & flag1) //only true if bit 1 is set for both ints
...
if(flags & flag2) //only true if bit 2 is set
..etc

}

Share this post


Link to post
Share on other sites
so flags holds multiple intigers? y do u need to say (flags & flag1)? y do they both need to be true?

Share this post


Link to post
Share on other sites
if(flags & flag1) evaluates to true if the flag1 bit was set. For instance:

flags = flag2 | flag3; // flag 1 not set so flags is 110 not 111

if(flags & flags1) // false because 110 & 001 is false

Share this post


Link to post
Share on other sites
Quote:
Original post by Yamian
so flags holds multiple intigers? y do u need to say (flags & flag1)? y do they both need to be true?


The single & is a bitwise AND. Only the bits that are set in both operands are kept.
12 & 8 = 8 . binary: 1100 & 1000 = 1000
8 & 8 = 8 . binary: 1000 & 1000 = 1000
12 & 4 = 4 . binary: 1100 & 0100 = 0100
8 & 4 = 0 . binary: 1000 & 0100 = 0000

So by choosing values for the flags that have only a single bit set, you can combine them using | (bitwise OR) and find out which ones are set using & (bitwise AND).

Share this post


Link to post
Share on other sites
hm, not sure if i understood that question. I guess you want to know what value is stored in flags?


int flag1 = 1; // binary: 0001
int flag2 = 2; // binary: 0010
int flag3 = 4; // binary: 0100

int flags1 = flag1 | flag2;
int flags2 = flag1 | flag3;
int flags3 = flag2 | flag3;



flags1 will be 3 (binary: 0011)
flags2 will be 5 (binary: 0101)
flags3 will be 6 (binary: 0110)

The actual value doesn't matter that much, you check if a flag is set using bitwise AND.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!