void SetBit(int mode, int flag)
{
mode = mode | flag;
}
void UnSetBit(int mode, int flag)
{
//Flag set?
if(mode & flag)
mode = mode ^ flag;
}
Setting and unsetting bits
Okay, I know this has been discussed many times, but the searchfunction is out of order
I have also read the article about bitwise operations here on GD.net, but it did not answer my question.
How do I write a function which sets bit in a mode-variable (int mode), and more important hod do i unset it?
I want to be able to do: SetBit(MyMode,OBJECT_VISIBLE)
And UnSetBit(MyMode, OBJECT_VISIBLE)
My guesses are this:
But maybe there is a simpler answer, or maybe I am not correct at all?
second function can be replaced with
mode = mode & ~flag;
which is more efficient because there's no jump.
or you can use
mode |= flag;
mode &= ~flag;
that's as easy as it gets.
and you want to pass mode by reference.
[edited by - niyaw on December 7, 2002 8:41:07 AM]
mode = mode & ~flag;
which is more efficient because there's no jump.
or you can use
mode |= flag;
mode &= ~flag;
that's as easy as it gets.
and you want to pass mode by reference.
[edited by - niyaw on December 7, 2002 8:41:07 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement