Passing options to a function...

Started by
6 comments, last by DigitalDelusion 19 years ago
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.
----------------------------------------------------------No matter how eloquently you state your argument, the fact remains that the toilet seat is a bistable device. Therefore it's natural position is no more down than it is up.[SDL Smooth Tile Scrolling]
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

HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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]
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
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
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".

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.
----------------------------------------------------------No matter how eloquently you state your argument, the fact remains that the toilet seat is a bistable device. Therefore it's natural position is no more down than it is up.[SDL Smooth Tile Scrolling]
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;.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
and flip a flag with flags ^= FLAGN;
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats

This topic is closed to new replies.

Advertisement