1

A school project requires me to perform basic set operations using my own bit-string implementation (use of STL is prohibited). I have a very basic but functional bit-vector wrapper set up and it works perfectly with all the set operations that can be calculated by the native C bit-wise operators (Bit-wise AND, OR, XOR) etc.

However Set Subtraction is the one required operation I can't figure out how to calculate using bit-string operations. Set Subtraction meaning (A - B) = all values that ARE in A but are NOT in B

Here is my implementation and two of the basic operations:

#include <iostream>
#include <cstdlib>
#include <vector>

#define WORDSIZE 9 // the sets will only ever contain numbers from 0 to 9
#define BIT_WS 5
#define MASK 0x1f

using namespace std;

int init_bitvector(int **bv, int val)
{
    *bv = (int*)calloc(val / WORDSIZE + 1, sizeof(int));
    return *bv != NULL;
}

void set(int bv[], int i)
{
    bv[i >> BIT_WS] |= (1 << (i & MASK));
}

int member(int bv[], int i)
{
    return bv[i >> BIT_WS] & (1 << (i & MASK));
}

int main()
{
    bool input_check = true; // Use to control user input
    int input_temp;
    int *bitvectorA, *bitvectorB, *bitvectorOR, *bitvectorAND, *bitvectorDIFF;

    vector<int> SetA;
    vector<int> SetB;

    init_bitvector(&bitvectorA, WORDSIZE);
    init_bitvector(&bitvectorB, WORDSIZE);
    init_bitvector(&bitvectorOR, WORDSIZE);
    init_bitvector(&bitvectorAND, WORDSIZE);
    init_bitvector(&bitvectorDIFF, WORDSIZE);

// ...user input for set values...

for (int i = 0; i < SetA.size(); i++)
{
    set(bitvectorA, SetA[i]);
}

for (int i = 0; i < SetB.size(); i++)
{
    set(bitvectorB, SetB[i]);
}

cout << endl << "Intersection of Set A and Set B:" << endl;

*bitvectorAND = (*bitvectorA & *bitvectorB);

for(int i = 0; i <= WORDSIZE; i++)
{
    if(member(bitvectorAND, i))
    {
        cout << i << ' ';
    }
}
cout << endl;

cout << endl << "Union of Set A and Set B:" << endl;

*bitvectorOR = (*bitvectorA | *bitvectorB);

for(int i = 0; i <= WORDSIZE; i++)
{
    if(member(bitvectorOR, i))
    {
        cout << i << ' ';
    }
}
cout << endl;

I can confirm that this works exactly as intended for all operations that have bit-wise operators. I just can't figure out how to implement Set Subtraction in a comparable manner.

user3776749
  • 667
  • 1
  • 10
  • 20
  • If you know how to add bitwise numbers, then take 2-s complement of the number to be subtracted, then add the two numbers together. – Sam Varshavchik Oct 16 '16 at 21:51
  • define what you mean by "set subtraction". and there you have your answer. – Cheers and hth. - Alf Oct 16 '16 at 21:54
  • @Cheersandhth.-Alf Set Subtraction (A - B) = all the values that ARE in A and ARE NOT in B. I can manually find it using a cumbersome for-loop, but I don't think that's the purpose of the assignment. – user3776749 Oct 16 '16 at 21:57
  • 1
    Well, that looks very much like `A & ~B` in ordinary C++ syntax. So you need to figure out a way to support complements. Unless you do that already. – Cheers and hth. - Alf Oct 16 '16 at 22:04
  • I can perform any bit-wise operation supported by C++, I just needed the logical leap. This did it, Thanks! – user3776749 Oct 16 '16 at 22:12

1 Answers1

1

Solution:

*bitvectorDIFF = (*bitvectorA & ~*bitvectorB);

Thanks to Cheers and hth. -Alf for the tip

Community
  • 1
  • 1
user3776749
  • 667
  • 1
  • 10
  • 20