user7187021
user7187021

Reputation:

Math ADD operation over vector of bools

So I created a class called myClass that takes in an int and has a private variable that stores the int as a vector in binary (i.e. 12 is '1100'). I want to define an operator that adds two myClass variables together as a vector of bools (aka bit-wise operation).

Here is the code I have:

class myClass {
public:
    myClass();
    myClass(int a);

    myClass& operator+(const myClass& value);

private:
    std::vector<bool> bit;
};

I want this to work in the main function:

int main() {
    std::cin >> value;
    Integer a = value;
    std::cin >> value;
    Integer b = value;
    myClass c = a+b;

    return 0;
}

Operator definition:

myClass myClass::operator+(const myClass& rhs) {
    Integer c = // not sure what to do here
    return c;
}

The part that's confusing me is that it must take in an integer but then the operator does the operation on the vector of bools.

Upvotes: 0

Views: 99

Answers (3)

Code-Apprentice
Code-Apprentice

Reputation: 83527

`myClass c = a+b;` 

Since a and b are both declared as Integer, this line will call operator+(const Integer& x, const Integer& y) or Integer::operator+(const Integer& x). The only way it will call myClass::operator+(const myClass& rhs) is if you have a conversion constructor myClass::myClass(const Integer& i).

Upvotes: 1

Tom&#225;š Zato
Tom&#225;š Zato

Reputation: 53129

Well obviously you need to do the same as when you add normal numbers on paper. Start with the lowest significance bits, and add them together. If the result overflows (eg. binary 1+1=10) then remember that overflow for the next iteration.

I'd strongly suggest that you first create constructor that takes bool array for your class:

myClass(std::vector<bool> bits);

We'll use that in the implementation. Now what you want is to add the lists of bools. I have created an implementation that doesn't care how big the lists are. This will be handy if you want to calculate with huge integers:

#include <vector>

bool add_bools(const bool A, const bool B) {
    return !(A && B) && (A || B);
}
/** Loops over vectors and adds the booleans in them
    the booleans are considered to be in little endian order
    so that the least significant is first in the array. **/
std::vector<bool> add_vectors(const std::vector<bool>& first,
                              const std::vector<bool>& second) {

    std::vector<bool> result;
    // Remembers that previous addition produced false when
    // we add true+true
    bool overflow = false;
    const int bits = first.size()>second.size()?first.size():second.size();
    for (int i = 0; i < bits || overflow; ++i) {
        bool bitA, bitB;
        bitA = i<first.size() ? first[i]:false;
        bitB = i<second.size() ? second[i]:false;
        bool tmp_result = add_bools(bitA, bitB);
        // remember to add overflow from previous iteration
        result.push_back(add_bools(tmp_result, overflow));
        // remember overflow for next iteration
        overflow = (bitA&&bitB) || (overflow && tmp_result);
    }
    return result;
}
#include <iostream>
void test_add_vectors() {
    std::vector<bool> first;
    std::vector<bool> second;
    const int bits = 5;
    for (int i = 0, l = bits; i < l; ++i) {
        first.push_back(false);
        second.push_back(true);
    }
    first[0] = true;

    std::vector<bool> result = add_vectors(first, second);
    for (int i = 0, l = result.size(); i < l; ++i) {
        std::cout<< (result[i]?'1':'0')<<" ";
    }
}

You can use that implementation like this, making use of the constructor that takes bool array:

myClass myClass::operator+(const myClass& rhs) {
    myClass result(add_vectors(bit, rhs.bit));
    return result;
}

Upvotes: 1

tinkertime
tinkertime

Reputation: 3042

You need to define a way to go to and from an integer representation. Here's a rough idea:

#include <vector>
#include <iostream>

class myClass {
private:
    void setInt(int x) {
        bit.clear();
        while (x) {
            if (x & 1)
                bit.push_back(1);
            else
                bit.push_back(0);
            x>>=1;  
        }
        reverse(bit.begin(), bit.end());
    }
public:
    int toInt() const {
        int i = 0;
        for (size_t b = 0; b < bit.size(); b++) {
            if (bit[bit.size() - 1 - b])
                i |= 1<<b;
        }

        return i;
    }

    myClass(int a) {
        setInt(a);
    }

    myClass& operator+(const myClass& value) {
        setInt(toInt() + value.toInt());
        return *this;
    }

private:
    std::vector<bool> bit;
};

int main() {
    myClass c(10);
    myClass d(20);
    std::cout << "c=" << c.toInt() << "\n";
    std::cout << "d=" << d.toInt() << "\n";
    std::cout << "Sum=" << (c + d).toInt() << "\n";
}

Upvotes: 1

Related Questions