Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Setting and unsetting bits

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

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:
  
void SetBit(int mode, int flag)
{
  mode = mode | flag;
}

void UnSetBit(int mode, int flag)
{
  //Flag set?

  if(mode & flag)
      mode = mode ^ flag;
}
  
But maybe there is a simpler answer, or maybe I am not correct at all?

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by niyaw ...and you want to pass mode by reference.
why?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
uh... nevermind

Share this post


Link to post
Share on other sites

  • 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!