Jump to content
  • Advertisement
Sign in to follow this  
Muzzy A

Bit Flag class

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

Hi,

I'm making a simple class that holds a 1 byte variable to use for 8 bit flags. But I don't know if I'm having a BIG LOOOONG brain fart or what, but i'm having problems lol.



class Flag
{
BYTE flags;
public:

/*** This works fine for checking if a bit is on or off, but what could I do about turning a bit on or off via the '[]' operator? ***/
bool operator[](int i)
{
// shift over to the bit we are checking for and check if it's on
if((flags >> i) & 1)
return true;

// The bit is turned off
return false;
}
};

Share this post


Link to post
Share on other sites
Advertisement
std::bitset<8>

You can use it. Or you can use it as a reference. Take your pick. Although my personal preference is not to re-implement trivial things like bitset. Edited by Washu

Share this post


Link to post
Share on other sites
I am really OCD when it comes to coding lol. I hate having code that looks cryptic so i simplify everything to the simplest form I can possibly get. =D.

so what does that do? toggle the 8th bit? and if i do 'bitset<5>' it will toggle the 5th bit?

Share this post


Link to post
Share on other sites

I am really OCD when it comes to coding lol. I hate having code that looks cryptic so i simplify everything to the simplest form I can possibly get. =D.


You better get over that if you ever plan on working with any other programmer ever biggrin.png

Share this post


Link to post
Share on other sites
First, you're probably not OCD. Second, if you were you would want your code to be as near to perfect as possible. In which case, building your own (probably buggy) implementation is probably Doing It Wrong.

Share this post


Link to post
Share on other sites
Using one of the built in integer types and enum or defines for defining fields is much safer and more portable, and generally runs the fastest. Most CPU's are horrible at bit shifting by a variable amount (i.e. an amount chosen at runtime instead of hardcoded at compile time)

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!