Question

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;
}
Was it helpful?

Solution

You already have basically the best solution: to make a proxy class which is assignable from a bool but internally only assigns 1 bit, and is implicitly convertible to a bool. BoolArray would return this proxy class from operator[].

In fact, this is what vector<bool> does with vector<bool>::reference. As commenters have said, std::vector is specialized for bool to do exactly what you are writing. So unless this is just for learning, what you're writing is useless/redundant.

OTHER TIPS

As commenters suggest you, use a std::bitset. Not reinvent the wheel.

But if you want to do an exercise, and implement a bitset, the answer is NO.

In fact, the implementation of std::bitset does exactly you are doing: Using a proxy class that stores the value and the position of the bit.

Please checkout lines 00760 (Proxy class declaration) and 01138 (Declaration and implementation of non-const operator[]).

Using a proxy object as you do, is basically the right thing to do. BUT: You should never use floating point operations to do integer arithmetic (They are inexact, and sometimes have to little precision):

// Don't do this:
int index = floor((double)bit_number/8);
// But this:
int index = bitnumber/8;

Integer division will alway round towards zero, using floor(x) is not needed.

// Don't do this:
int binary_num = pow(2,bit_number);
// But this:
int binary_num = 1 << bit_number;

In addition to potentially giving wrong result after rounding to integers, pow(a,b) is much slower than using the bit-shift operator.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top