class CBitArray
{
public:
CBitArray();
CBitArray(int n);
CBitArray(char * p, int length);
virtual ~CBitArray();
void CreateArray(int size);
int GetSize();
void SetValue(unsigned int position, int value);
int GetValue(unsigned int position);
private:
unsigned int m_nSize;
char *m_pArray;
};
CBitArray::CBitArray()
{
m_nSize=0;
m_pArray=0;
}
CBitArray::CBitArray(int n)
{
m_pArray=0;
CreateArray(n);
}
CBitArray::CBitArray(char* stream, int length)
{
m_nSize=length*8;
m_pArray=new char[length];
for(int i=0;i<length;i++) m_pArray[i]=stream[i];
}
CBitArray::~CBitArray()
{
if(m_pArray) delete [] m_pArray;
}
void CBitArray::CreateArray(int size) {
if(m_pArray) delete [] m_pArray;
m_pArray=new char[size/8+1];
for(int i=0;i<size/8+1;i++) m_pArray[i]=0;
m_nSize=size;
}
int CBitArray::GetSize() {
return m_nSize;
}
void CBitArray::SetValue(unsigned int position, int value) {
if(position >= m_nSize) return;
if(value%2) m_pArray[position/8]|=1<<position%8;
else m_pArray[position/8]&=-1^1<<position%8;
}
int CBitArray::GetValue(unsigned int position) {
if(position >= m_nSize) return 0;
return (m_pArray[position/8]&1<<position%8)>>position%8;
}
------
길이÷8 바이트를 사용하는 비트어레이