I'm trying to make a BoolArray class which represent an array of booleans but instead of save 1 boolean value per bool variable. it's uses 1 char variable to represents 8 booleans values using bits, and save memory. I want to make it convenient as normal array using operator [].
I can use arr[5] and return a bool value for the fifth bit. my problem is the assignment of a bit. I cant make a reference to one bit, so I need to make a function that perform that task. My question is if it possible to control array style assignment using operators overloading. My solution was to use another class(MyBoolean) and return it as a reference to BoolArray operator[]. MyBoolean overloads operator= and change the bits of BoolArray. Is there a simpler way That overloads kind of operator[]= or something like that?
#include <iostream>
#include <tgmath.h>
using namespace std;
string int_to_binary_string(int number)
{
if ( number == 0 ) return "0";
if ( number == 1 ) return "1";
if ( number % 2 == 0 )
return int_to_binary_string(number / 2) + "0";
else
return int_to_binary_string(number / 2) + "1";
}
class MyBoolean;
class BoolArray {
private:
unsigned char* arr;
int size;
void setBit(int bit_number,bool value);
public:
explicit BoolArray(int size);
~BoolArray();
MyBoolean operator[](unsigned int index);
int getSize() const;
friend class MyBoolean;
};
// ***************************** MyBoolean *********************************
class MyBoolean {
private:
bool value;
int bit_number;
BoolArray* bool_array;
public:
// --- Constructor ---
MyBoolean(bool value,int bit_number,BoolArray* bool_array) :
value(value),bit_number(bit_number),bool_array(bool_array)
{}
// --- cast ---
operator bool() const{
return value;
}
MyBoolean& operator=(bool new_value) {
value = new_value;
bool_array->setBit(bit_number,new_value);
}
};
// ***************************** BoolArray *****************************
void BoolArray::setBit(int bit_number,bool value) {
int index = floor((double)bit_number/8);
bit_number -= index*8;
cout << "trying to set " << bit_number << " to " << value << endl;
if(value==true) {
int binary_num = pow(2,bit_number);
cout << "arr[index]: " << int_to_binary_string(arr[index]) << " - binary_num: " << int_to_binary_string(binary_num);
arr[index] = arr[index] | binary_num;
cout << " - after: " << int_to_binary_string(arr[index]) << endl;
}
else {
int binary_num = 0 | 255;
int binary_num_2 = pow(2,bit_number);
binary_num = binary_num^binary_num_2;
arr[index] = arr[index] & binary_num;
}
}
// --- Constructor ---
BoolArray::BoolArray(int size) :
size(size)
{
size = ceil((double)size/8);
arr = new unsigned char[size];
}
// --- Destructor ---
BoolArray::~BoolArray() {
delete[] arr;
}
// --- operator[] ---
MyBoolean BoolArray::operator[](unsigned int index) {
if(index>size-1)
throw "error";
int arr_index = floor((double)index/8);
int bit_number = index - arr_index*8;
unsigned int binary_num = pow(2,bit_number);
int value= false;
if((arr[arr_index] & binary_num)>0)
value = true;
MyBoolean my_bool(value,index,this);
return my_bool;
}
// --- size ---
int BoolArray::getSize() const {
return size;
}
ostream& operator<<(ostream& os, MyBoolean b) {
os << (bool) b;
return os;
}
ostream& operator<<(ostream& os,BoolArray& arr) {
for(int i=0;i<arr.getSize();i++)
os << arr[i] << "->";
return os;
}
int main() {
BoolArray arr(12);
arr[0] = true;
arr[1] = false;
arr[2] = true;
arr[3] = false;
arr[4] = true;
arr[5] = false;
arr[6] = true;
arr[7] = false;
arr[8] = true;
arr[9] = false;
arr[10] = true;
arr[11] = false;
cout << arr;
return 0;
}