#define ALLOW_INSTRUCTOR_CODE /*************************************************************************** *************************************************************************** ****** BEFORE BEGINNING OR TURNING IN THIS EXERCISE IT IS EXTREMELY ****** ****** IMPORTANT THAT YOU READ, FULLY UNDERSTAND, AND ADHERE TO THE ****** ****** REQUIREMENTS STATED IN THE DOCUMENT TITLED, "ASSIGNMENT ****** ****** SUBMISSION REQUIREMENTS", DOWNLOADABLE FROM THE COURSE WEB ****** ****** SITE. ****** *************************************************************************** **************************************************************************/ /*************************************************************************** ************* C/C++ Programming 2: Assignment 2 - Exercise 2 ************* ******** BEGIN EXERCISE REQUIREMENT - THIS MUST BE A "C++" PROGRAM ******** *************************************************************************** (3 points) Write function Rotate: Rotate syntax: unsigned Rotate(unsigned object, int count); Parameters: object - the object to rotate count - the number of bit positions and direction to rotate, where: negative=>left and positive=>right Synopsis: Rotates ALL bits in by the number of bit positions and in the direction specified by . Return: the value of the rotated object By definition a bit rotation requires that the least significant bit (lsb) and the most significant bit (msb) of an arithmetic object be treated as if they are adjacent. That is, when an n-bit object is right-rotated the lsb is placed into the msb rather than being thrown away, as would be the case with a right shift: |<--------------------------------------------| | | | ------------------------------------- | |--> | msb | | | | | lsb | -->| ------------------------------------- Right Rotate Conversely, when an arithmetic object is left-rotated the msb is placed into the lsb, rather than being thrown away as would be the case with a left shift: |-------------------------------------------->| | | | ------------------------------------- | |<-- | msb | | | | | lsb | <--| ------------------------------------- Left Rotate Examples, for a 16-bit object: Rotate(0xA701, 1) returns 0xD380 Rotate(0xA701, 256) returns 0xA701 Rotate(0x000C, 2) returns 0x0003 Rotate(0x8000, -1) returns 0x0001 Rotate(0x3000, -2) returns 0xC000 Rotate must: * Work on any machine, regardless of the number of bits in the data type of parameter . * Handle rotation counts greater than the number of bits in the data type of parameter . * Do no displaying. *************************************************************************** ************************ END EXERCISE REQUIREMENT ************************* **************************************************************************/ /*************************************************************************** * BEFORE PRINTING THIS ASSIGNMENT TO TURN IN, DELETE THIS COMMENT AND * EVERYTHING ABOVE IT. **************************************************************************/ /* * TODO: * REPLACE THIS COMMENT WITH YOUR TITLE BLOCK. */ /* * TODO: * REPLACE THIS COMMENT WITH YOUR C++ CODE, PLUS ANYTHING NECESSARY TO * SUPPORT MY TEST CODE BELOW. DO NOT INCLUDE ANY UNNEEDED HEADER FILES! */ /*************************************************************************** * BEFORE PRINTING THIS ASSIGNMENT TO TURN IN, DELETE THIS COMMENT AND * EVERYTHING BELOW IT. **************************************************************************/ /* ***** Change nothing below this comment without instructor permission. **** * Everything that follows was written to help test/verify your code. You * do not need to understand the details of my code to write yours! */ #ifdef ALLOW_INSTRUCTOR_CODE int main() { // // Note: Displaying rotation or shift counts in any radix other than // decimal is difficult to interpret, but displaying bit patterns in // decimal is virtually useless. // cout << hex; // Test several different cases. cout << "0x5 rotated right by 1 bit is 0x" << Rotate(0x5, 1) << "\n\n"; cout << "0x5 rotated left by 1 bit is 0x" << Rotate(0x5, -1) << "\n\n"; cout << "0x5 rotated right by 64 bits is 0x" << Rotate(0x5, 64) << "\n\n"; cout << "0x8765 rotated left by 64 bits is 0x" << Rotate(0x8765, -64) << "\n\n"; cout << "0x8765 rotated right by 3217 bits is 0x" << Rotate(0x8765, 3217) << "\n\n"; cout << "0x8765 rotated left by 3217 bits is 0x" << Rotate(0x8765, -3217) << "\n\n"; return EXIT_SUCCESS; } #endif