#include <iostream>

using namespace std;

class BArray
{
    int *bits;
    unsigned size;
    static const unsigned sizeofint;
    class Lv
    {
        BArray &array;
        unsigned pos;
      public:
        Lv(BArray &who, unsigned pos);
        void setbit(bool v);
        bool getbit(void) const;
        operator bool(void) const;
        bool operator = (bool v);
	bool operator = (const Lv &v);
    };
    void setbit(unsigned pos, bool v);
    bool getbit(unsigned pos) const;

  public:
    class iterator
    {
	BArray *array;
	unsigned pos;
      public:
	iterator(BArray &who, unsigned pos);
	iterator(void);
	bool operator == (const iterator &v) const;
	Lv operator *(void);
	void operator ++(int);
    };

    Lv operator[] (unsigned pos);
    BArray(unsigned int size);
    ~BArray(void);
    iterator begin(void);
    iterator end(void);
};

const unsigned BArray::sizeofint = sizeof(int)*8;

BArray::BArray(unsigned int size)
{
  bits = new int[(size + sizeofint - 1)/sizeofint];
  this->size = size;
}

BArray::~BArray(void)
{
  delete bits;
}

void BArray::setbit(unsigned pos, bool v)
{
  if (pos < size)
  {
    if (v)
    {
      bits[pos / sizeofint] |= 1 << (pos % sizeofint);
    }
    else
    {
      bits[pos / sizeofint] &= ~(1 << (pos % sizeofint));
    }
  }
}

bool BArray::getbit(unsigned pos) const
{
  bool result = false;
  if (pos < size)
  {
    return bits[pos / sizeofint] & (1 << (pos % sizeofint));
  }
}

BArray::Lv::Lv(BArray &who, unsigned p):array(who),pos(p)
{
}

void BArray::Lv::setbit(bool v)
{
  array.setbit(pos,v);
}

bool BArray::Lv::getbit(void) const
{
  return array.getbit(pos);
}

BArray::Lv::operator bool(void) const
{
  return getbit();
}

bool BArray::Lv::operator = (bool v)
{
  setbit(v);
  return getbit();
}

bool BArray::Lv::operator = (const BArray::Lv &v)
{
  setbit((bool)v);
  return getbit();
}

BArray::Lv BArray::operator[] (unsigned pos)
{
  return Lv(*this, pos);
}

int main(void)
{
	BArray ba(20);
	bool v1;

	ba[0] = true;
	ba[1] = ba[0];
	v1 = ba[1];
	ba[0] = false;
	cout << ba[0] << " " << ba[1] << endl;
{
  BArray ba(20);

  BArray::iterator i;

  for (i = ba.begin(); !(i == ba.end()); i++) 
  {
    cout << (bool)*i << endl;
  }
}

}
BArray::iterator::iterator(BArray &who, unsigned p):array(&who),pos(p)
{
}

BArray::iterator::iterator(void):array(0),pos(0)
{
}

bool BArray::iterator::operator == (const BArray::iterator &v) const
{
  return (array == v.array) && (pos == v.pos);
}

BArray::Lv BArray::iterator::operator *(void)
{
  return Lv(*array, pos);
}

void BArray::iterator::operator ++(int)
{
  pos++;
}

BArray::iterator BArray::begin(void)
{
  return BArray::iterator(*this, 0);
}

BArray::iterator BArray::end(void)
{
  return BArray::iterator(*this, size);
}


