• Create Account

## Bit Flag class

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1Muzzy A  Members

737
Like
0Likes
Like

Posted 01 May 2012 - 11:32 PM

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



### #2Washu  Senior Moderators

7714
Like
0Likes
Like

Posted 01 May 2012 - 11:38 PM

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, 01 May 2012 - 11:41 PM.

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.
ScapeCode - Blog | SlimDX

### #3Muzzy A  Members

737
Like
0Likes
Like

Posted 01 May 2012 - 11:53 PM

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?

### #4Washu  Senior Moderators

7714
Like
0Likes
Like

Posted 02 May 2012 - 01:01 AM

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.
ScapeCode - Blog | SlimDX

### #5turch  Members

590
Like
0Likes
Like

Posted 02 May 2012 - 07:58 AM

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

### #6FLeBlanc  Members

3133
Like
1Likes
Like

Posted 02 May 2012 - 08:18 AM

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.

### #7Zoner  Members

232
Like
0Likes
Like

Posted 04 May 2012 - 07:22 PM

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)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.