Answered step by step
Verified Expert Solution
Link Copied!

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 1 that a Byte consists of 8-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 16-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 0
BitsWord(int 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 setValue(int 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 8-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 8-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 8-bits. You can simply call the setValue function on mUpper passing value >>8.
Accessors
getLower
C++-*BitsByte getLower()
Java - BitsByte getLower()
returns the lower byte. It should be called mLower
getUpper
C++-*BitsByte 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 16-bit binary number.
toInt
Returns the values contained in mLower and mUpper put together as one 16-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 0. Next, OR the variable with the value produced by toInt on mUpper and left shift this value by 8 bits. Something like this val |= mUpper.toInt()<<8
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;
bw.setValue(259);
cout << bw.getUpper()->toString()<< endl;
cout << bw.getLower()->toString()<< endl;
cout << bw.toInt()<< endl;
cout << bw.toString()<< endl;
return 0;
} Output
00000001
00000011
259
0000000100000011
Task 2- 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 num1(35);
BitsWord num2(10);
BitsWord *num3= MathWord::add(num1, num2);
cout << "Add: "<< num3->toInt()<< endl;
num3= MathWord::sub(num1, num2);
cout << "Sub: "<< num3->toInt()<< endl;
num3= MathWord::mul(num1, num2);
cout << "Mul: "<< num3->toInt()<< endl;
num3= MathWord::div(num1, num2);
cout << "Div: "<< num3->toInt()<< endl;
num3= MathWord::max(num1, num2);
cout << "Max: "<< num3->toInt()<< endl;
num3= MathWord::min(num1, num2);
cout << "Min: "<< num3->toInt()<< endl;
return 0;
}
Caveat: We should probably delete the num3 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Find the derivative of y= cos cos (x + 2x)

Answered: 1 week ago