Jump to content
  • Advertisement
Sign in to follow this  
wasted_druid

Passing options to a function...

This topic is 4804 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

I see functions where you pass you options seperated by a '|' character... such as : SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO|SDL_INIT_TIMER) My question is how I would code that myself. I'm assuming they're bit operators or some such... SDL_INIT_AUDIO = 1 SDL_INIT_VIDEO = 2 SDL_INIT_TIMER = 4 or some such... In other words, what exactly is happening in the above statement? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
yup you got the right idea '|' in C and C++ is the bitwise or operator so the values are simple ored togheter and sent to the function.

The callee can then check the options using '&' (binary and) like so

if( options & SDL_INIT_AUDIO)//do audio init

Share this post


Link to post
Share on other sites
The | is a bitwise OR operator. It is being used here to concatenate bits together. If you have an 8 bit number for the flags, you could set them with OR. For example 00000000 | 00000010 | 00100000 = 00100010

Also, note that that whole things you are passing to the function is just one argument that you are building not three arguments.

Other bitwise operators include & for AND, ~ for NOT, and ^ for XOR. Not to be confused with boolean operators &&, ||, and !

[Edited by - Omnibus on March 31, 2005 3:47:30 PM]

Share this post


Link to post
Share on other sites
As the others have explained this well, I'll not bore you with more of the same, just figured I'd mention that those are generally refered to as flags, not options [smile] You'll get blank looks, methinks, if you go around spouting about 'options' :D

Share this post


Link to post
Share on other sites
Just a little addition to the topic.

You can easily create these flags using the bit shift operator. Eg:

(1 << 0) == 0000001
(1 << 1) == 0000010
(1 << 2) == 0000100
(1 << 3) == 0001000


etc... I just find it a bit easier than writing "#define FLAG1 2", "#define flag16 65536".

Share this post


Link to post
Share on other sites
Quote:
Original post by SirLuthor
As the others have explained this well, I'll not bore you with more of the same, just figured I'd mention that those are generally refered to as flags, not options [smile] You'll get blank looks, methinks, if you go around spouting about 'options' :D


Thankyou. I hated asking the question since I didn't know the terminology. Heck, if I'd known the right term, I would have googled for it.

Anyways, thanks again.

Share this post


Link to post
Share on other sites
Since nobody has said this yet, you can set a flag with flags |= FLAGN;, test if a flag is set, something like if(flags & FLAGN) {...}, and unset a flag with something like flags &= ~FLAGN;.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!