Answered step by step
Verified Expert Solution
Question
1 Approved Answer
We saw in project 1 that a Byte consists of 8 - bits. We even created a BitsByte class which allows us to do things
We saw in project that a Byte consists of bits. We even created a BitsByte class which allows us to do things with a Byte value. In this assignment you are going to create the BitsWord class that is composed of two BitsByte classes. In this way you can then create a bit Word which consists of two Bytes.
In the private section of the class create two BitsByte variables. One called mLower and the other called mUpper. As you have probably guessed one will hold the upper Byte of the word and the other will hold the lower Byte of the Word.
C You should create pointers to these variables and initially set them to nullptr. Constructors
BitsWord Default constructor that sets the values of mUpper and mLower to
BitsWordint value Overloaded constructor that sets mUpper and mLower to the first two bytes of value.
You should keep one code path as much as possible. This means delegating constructors and using methods that have been written
C You should create a destructor that will delete the objects create with new. Make sure you check that memory was allocated before you attempt to delete the pointer.
Mutator
void setValueint value sets mUpper and mLower to the first two bytes of value
First set mLower by simply calling the setValue function on mLower passing value. Because we are only using the first bits we don't need to do anything to the number at this point.
To get the upper byte from value you are going to have to do some bit shifting. If you think about it the first binary number of the upper byte in value is bits away from the first binary number in the lower byte of value. This means, you are going to have to shift value right by bits. You can simply call the setValue function on mUpper passing value
Accessors
getLower
CBitsByte getLower
Java BitsByte getLower
returns the lower byte. It should be called mLower
getUpper
CBitsByte getUpper
Java BitsByte getUpper
returns the lower byte. It should be called mUpper
toString returns a string that contains the binary value of mLower and mUpper as one bit binary number.
toInt
Returns the values contained in mLower and mUpper put together as one bit int value.
To create the int value you are going to have to do the reverse of setValue. Create an int variable and set it to Next, OR the variable with the value produced by toInt on mUpper and left shift this value by bits. Something like this val mUpper.toInt
To add in the lower bits you simply OR the variable with the value produced by toInt on mLower. Something like this val mLower.toInt
Here is some code that you can use to test what you have written to this point: C
int main
BitsWord bw;
bwsetValue;
cout bwgetUppertoString endl;
cout bwgetLowertoString endl;
cout bwtoInt endl;
cout bwtoString endl;
return ;
Output
Task The MathWord class
Create a static public class called MathWord that can be used to do some simple arithmetic on the BitsWord class.
Functionality
add adds two BitsWord classes together and returns the sum as a new BitsWord class
sub subtracts two BitsWord classes from each other and returns the difference as a new BitsWord class
mul Multiplies two BitsWord classes together and returns the product as a new BitsWord class
div Divides two BitsWord classes together and returns the quotient as a new BitsWord class
max Takes as an argument two BitsWord classes and return the one that contains the larger value as a new BitsWord class.
min Takes as an argument two BitsWord classes and return the one that contains the smaller value as a new BitsWord class.
C Your functions should return a pointer to BitsWord classes.
Here is a small piece of test code that you can use to check your classes:
C
int main
BitsWord num;
BitsWord num;
BitsWord num MathWord::addnum num;
cout "Add: numtoInt endl;
num MathWord::subnum num;
cout "Sub: numtoInt endl;
num MathWord::mulnum num;
cout "Mul: numtoInt endl;
num MathWord::divnum num;
cout "Div: numtoInt endl;
num MathWord::maxnum num;
cout "Max: numtoInt endl;
num MathWord::minnum num;
cout "Min: numtoInt endl;
return ;
Caveat: We should probably delete the num pointer before reassigning it What we are creating here is a memory leak. This isn't a big problem in this case because this is done in main and the memory is wiped out when the program exits. It is important to keep this in mind.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started