Sign in to follow this  

Bit Flag class

This topic is 2047 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.

[code]

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;
}
};

[/code]

Share this post


Link to post
Share on other sites
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
[quote name='Muzzy A' timestamp='1335938027' post='4936671']
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.
[/quote]

You better get over that if you ever plan on working with any other programmer ever [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

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

This topic is 2047 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this