Question: I have this code which I am getting stuck on: #include #include #include #include #include #include using namespace std; class Bitset { private: vector StoBits;

I have this code which I am getting stuck on:

#include #include #include #include #include #include

using namespace std;

class Bitset { private: vector StoBits;

public: //1 Constructor resizes Vector to 1 void ResetBit(); //2 TEST accessor, allows user to test a specified bit bool Test(const int&); //3 SET mutator, allows user to set the nth bit and grows Bitset accordingly void Set(const int&); //4 CLEAR mutator, allows user to clear the nth bit and shrinks Bitset accordingly void Clear(const int&); //5 Get Set accessor, takes set # and returns set in bits string GetSet(what goes here?); //6 Get Num Sets accessor, returns # of sets int GetNumSets(); };

int main (){ //Vector initialization ResetBit(); //Internal Variables char Menu; int BitNumber;

do{ if (cin.eof()){ break;} Menu = 'a'; BitNumber = 0; //Menu Prompt cout<< "Choose a menu option "; cout<< "Type the option, press enter, then specifiy numbers. "; cout<< "Type t followed by a number to Test a bit. "; cout<< "Type s followed by a number to Set a bit to 1. "; cout<< "Type c followed by a number to Clear a bit. "; cout<< "To display a bit set at number x, with y spaces between bits, Type g followed by 'x y'. "; cout<< "Type n to return number of Bit Sets. "; cout<< "Type q to quit. "; cin >> Menu;

//Test Option if (Menu == 't'){ cout << " Which bit would you like to Test? "; cin >> BitNumber;

} //Set Option if (Menu == 's'){ cout << " Which bit would you like to Set? "; cin >> BitNumber; } //Clear Option if (Menu == 'c'){ cout << " Which bit would you like to Clear? "; cin >> BitNumber; } //Get Set Option if (Menu == 'g'){ cout << " Will display the xth set with spaces at every yth bit. 'x y' ? "; getline (cin, GetSetInput); gs.str(GetSetInput); gs >>

//Gives # of Bit Sets (# of ints) if (Menu == 'n'){

}

//Quits if (Menu == 'q'){ break; } //Contingency else { cout << " Invalid response, please try again: "; }

} while(!cin.eof());

return 0; }

//Functions

//1 Constructor (c) Bitset::ResetBit(){ StoBits.resize(1); }

//2 Test Accessor (t) Bitset::Test(const int& BitNumber){

}

//3 Set Mutator (s) Bitset::Set(const int& BitNumber){

}

//4 Clear Mutator (c) Bitset::Clear(const int& BitNumber){

} //5 Get Set (g) Bitset::GetSet(what goes here? )

//6 Get Num Sets accessor (n) Bitset::GetNumSets(){

}

What I'm having trouble with is bitwise operators. For this assignment, we cannot do math with ints.

I need help finishing my functions. Here is the prompt for the functions:

The following members must be in a class called BITSET:

1. A private vector of integers. This will be where the actual bitset data is stored. The user may specify any integer as a bit, and your class must handle them transparently. Notice that this is a vector of integers, so there are only 32 bits per set. So, if the user specifies 66 as the bit that they want to set, that would be bit 2 (third bit from the right) of the third set (set #2). To find the set, think about the bit that needs to be set. If the user specifies 32, thats bigger than the first set, which has indices 0 - 31. So, it must be the first bit of the second set. Think about 66. The first set stores 0 - 31, the second set stores 32-63, and the third set stores 64-95. Therefore, it would be bit--64...65...66--the third bit from the right in set #2 (third set). Just like arrays, set numbers and bit numbers are 0-based indexed.

2. A public 0-argument constructor for the BITSET class. This constructor will resize the private vector of integers to the exact size 1 (see vector.resize() functionLinks to an external site.). ALL newly created sets must have the set equal to 0 (all bits cleared).

3. A public accessor function called Test. This function will return true or false given an integer location. This function will test the given location. If that bit is 1, return true, otherwise return false. Remember, the location can be any integer. So, your Test function must be able to look at multiple sets. For example, if I call Test(62) with the following sets, I will get true, which means that bit #62 is 1. Whereas, if I call Test(65) with the following sets, I will get false, which means that bit #65 is 0. If the set is not present, return 0. Since we are trying to represent an infinite number of bits using a resizable vector, any bit tested that is above what actually exists will be 0. For example, if the size of the vector is 4, then Test with any parameter > 127 will return 0, since set index 3 [the fourth set] will represent up to and including bit 127.

Set 0 [31..0] : 0000_0000_0000_0000_1100_0000_1110_1000 Set 1 [63..32] : 0110_0111_0000_0000_0000_0000_0000_0001 Set 2 [95..64] : 0000_0000_0000_0011_0000_0000_0110_0101 Set 3 [127..96]: 0000_0000_1111_1110_0000_0000_0000_0001

4. A public mutator function called Set. This function will return nothing and take an integer location. This function will set the given bit to 1. Notice that you originally set the number of sets to 1 in the constructor. Your Set() function must be able to "grow" the vector should it need to. For example, if I specify bit 39, that exceeds the first set, which stores bits 0-31. So, you need to grow the vector by adding an additional set. By default, all bits in a set will be 0. Bit 39 would be the 8th bit (bit #7) in the second set. If the given bit is already 1, this function has no effect.

Before Set(39) call:

Set 0 [31..0] : 0000_0000_0000_0000_0000_0000_0000_0000

After Set(39) call:

Set 0 [31..0] : 0000_0000_0000_0000_0000_0000_0000_0000 Set 1 [63..32]: 0000_0000_0000_0000_0000_0000_1000_0000

5. A public mutator function called Clear. This function will return nothing and take an integer location. This function is the opposite of Set() and will set the given bit to 0. If the Clear() function must also "shrink" the set vector if the upper sets have been cleared to 0. The following illustrates a scenario with the function call to Clear(98):

Before Clear(98):

Set 0 [31..0] : 0000_0000_0000_0000_1100_0000_1110_1000 Set 1 [63..32] : 0000_0000_0000_0000_0000_0000_0000_0000 Set 2 [95..64] : 0000_0000_0000_0000_0000_0000_0000_0000 Set 3 [127..96]: 0000_0000_0000_0000_0000_0000_0000_0100

After Clear(98):

Set 0 [31..0] : 0000_0000_0000_0000_1100_0000_1110_1000

Notice that the Clear() function pruned the vector by resizing it from a size of 4 to a size of 1. Think carefully on how to test that a set is 0. Efficiency will be graded for this function.

6. A public accessor function called GetNumSets(). This function will take no parameters and will return the number of sets as an integer.

7. A public accessor function called GetSet(). This function will take a set index as a parameter and will return the entire set as a single, 4-byte integer. If the provided set doesn't exist, return 0.

8. You will be writing a non-member function called ToBinary. This function returns a string and takes two integers, a value, and spacing. This function will return the binary representation of the integer "value" as a string. The spacing integer determines how many binary digits will be placed into the string before a space is added. For example:

ToBinary(122, 4) would return a string "0000 0000 0000 0000 0000 0000 0111 1010". Notice that spaces are added after FOUR (4) binary digits, since this was specified as the "spacing" parameter. Also, notice that even though there are 4 bits at the very end (0b1010), there is no additional space added. You must handle this special case! The spacing must be added starting from the most significant digit. If the user gives a number that is not a factor of 32, then you will have digits "left over". The "left over" digits must be the least significant digits.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!