In this assignment, you write a C++ programfor logical cyclic shift of n-bit num
ID: 3665688 • Letter: I
Question
In this assignment, you write a C++ programfor logical cyclic shift of n-bit numbers by given number of positions (<= n).
You are required to create a circular list to store a given binary number. Then your program will be able to rotate the number left by any given number of positions for the cyclic left operation, and rotate the number by right by a givennumber of positions for the cyclic right operation. The provided sample input below elaborates on these operations.
In this assignment you are required to use a circular array to create a circular representation for the n-bit number. The cyclic shift operations will change where the first bit starts in the number. The cyclic shift left by one position shifts every bit to the left by one position; and the leftmost bit moves cyclically to the vacated rightmost position. Similarly, the cyclic shift right shifts every bit to the rightby one position; and the rightmost bit is moved cyclically to the vacated leftmost position. These generalize to cyclic shifts (left and right) by any given number of positions in an obvious way.
/* first input n= */ 8
/* next inputs are n bits, from leftmost bit to rightmost bit=*/ 11110000
L 1 /* rotate left by one position */
result=11100001
L 2 /* rotate left by two positions*/
result=10000111
L 1 /* rotate left */
result=00001111
R 3/* rotate right */
result=11100001
The above is one simple example your program must be designed to work for every n and every n-bit number, and for every number of positions (<= n).
Explanation / Answer
#include <iostream>
using namespace std;
template <typename B>
inline B rotateCarryRightmost( B operateMe, unsigned char numberShift )
{
static const unsigned char BCountBit =sizeof( B )*8U ;
return ( operateMe>> numberShift ) | ( operateMe<< ( BCountBit-numberShift ) ) ;
}
template <typename B>
inline B rotateCarryLeftmost( B operateMe, unsigned char numberShift )
{
static const unsigned char BCountBit=sizeof( B )*8U ;
return ( operateMe<<numberShift ) | (operateMe>>( BCountBit - numberShift ) ) ;
}
void main()
{
cout<<" unsigned-char" <<20U <<"rotated rightmost by-6 bits== "<< +rotateCarryRightmost<unsigned char> ( 20U , 6U)<<" " ;
cout<<" unsigned-char" <<20U <<"rotated leftmost by-6 bits== "<< +rotateCarryLeftmost<unsigned char> ( 20U , 6U)<<" " ;
cout<<" " ;
for( unsigned char numberShift= 0U ;numberShift <=sizeof( unsigned char )* 8U ;++numberShift )
{
cout<<" unsigned-char" <<21U <<"rotated rightmost by"<< +numberShift<<"bit( s )== " <<+rotateCarryRightmost<unsigned char> ( 21U , numberShift )<<" " ;
}
cout <<" " ;
for( unsigned char numberShift= 0U ;numberShift <=sizeof( unsigned char )* 8U ;++numberShift )
{
cout<<" unsigned-char" <<21U <<" rotated leftmost by"<< +numberShift<<"bit( s )== " <<+rotateCarryLeftmost<unsigned char> ( 21U , numberShift )<<" " ;
}
cout<<" " ;
for( unsigned char numberShift= 0U ;numberShift <=sizeof( unsigned long long )*8U ; ++numberShift )
{
cout<<"unsigned long-long"<< 3457347ULL<<"rotated rightmost by" <<+numberShift <<"bit( s )== " << rotateCarryRightmost<unsigned long long> ( 3457347ULL , numberShift )<<" " ;
}
cout<< " ";
for( unsigned char numberShift= 0U ;numberShift <=sizeof( unsigned long long )*8U ; ++numberShift )
{
cout<<"unsigned long-long" << 3457347ULL<<"rotated leftmost by" <<+numberShift <<"bit( s )== " << rotateCarryLeftmost<unsigned long long> ( 3457347ULL , numberShift)<<" " ;
}
cout<<" " ;
system( " pause " ) ;
}