• Create Account

### #ActualMisery

Posted 27 June 2012 - 01:25 AM

Hello,

I am in a need of creating a bitset. However I cannot think of any solution how to make an operator that would set the proper bit to true or false in such manner:

bitset(i)=true;
bitset(j)=false;


where i, j are the bits i want to set to proper value.

I have checked BOOST dynamic bitset, however I cannot figure out the soution. I know that I have to use some helper class. This is where I'm stuck. Very simplified, the problem is:

class Block  //helper class
{
public:
int Container;

Block& operator=(bool x)  { Assign(x);   return *this; }

void Assign(bool x) { x? do_set() : do_reset(); }
void do_set() { Container |= mask; }
void do_reset() { Container &= ~mask; }
};

class bitset
{
public:
int Data[4]; //array of ints - here only 4
int cs;  //container size

bitset()
{
cs=sizeof(int)*8;
}

int operator () (int i) const
{
return Data[i/cs] & (1 << (i%cs));
}

int& operator () (int i) //int, bool or whatever, Block?? I have no idea
{
//what do I add here to be able to do substitution like mentioned above?
}
};


Thanks in advance for any sugestions,
Misery

### #1Misery

Posted 27 June 2012 - 01:23 AM

Hello,

I am in a need of creating a bitset. However I cannot think of any solution how to make an operator that would set the proper bit to true or false in such manner:

bitset(i)=true;
bitset(j)=false;


where i, j are the bits i want to set to proper value.

I have checked BOOST dynamic bitset, however I cannot figure out the soution. I know that I have to use some helper class. This is where I'm stuck. Very simplified, the problem is:

class Block  //helper class
{
public:
int Container;

Block& operator=(bool x)  { Assign(x);   return *this; }

void Assign(bool x) { x? do_set() : do_reset(); }
void do_set() { Container |= mask; }
void do_reset() { Container &= ~mask; }
};

class bitset
{
public:
int Data[4]; //array of ints - here only 4
int cs;  //container size

bitset()
{
cs=sizeof(int)*8;
}

int operator () (int i) const
{
return Data[i/cs] & (1 << (i%cs));
}

int& operator () (int i)
{
//what do I add here to be able to do substitution like mentioned above?
}
};


Thanks in advance for any sugestions,
Misery

PARTNERS