Passing options to a function...
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.
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
The callee can then check the options using '&' (binary and) like so
if( options & SDL_INIT_AUDIO)//do audio init
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]
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
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".
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.
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;.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement