Bit Flag class

Started by
5 comments, last by Zoner 11 years, 11 months ago
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;
}
};

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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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?
http://lmgtfy.com/?q=std%3A%3Abitset+reference

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.


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
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.
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)
http://www.gearboxsoftware.com/

This topic is closed to new replies.

Advertisement