write a C++ program for logical cyclic shift of n-bit numbers by given number of
ID: 3667102 • Letter: W
Question
write a C++ program for 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 given number 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 linked list 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 right by 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.
You may choose to use doubly-linked circular linked list, or make a full circular traversal on each cyclic shift as we discussed in the lecture.
/* first input n = */ 8
/* next inputs are n bits, from leftmost bit to rightmost bit =*/ 1 1 1 1 0 0 0 0
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
C++ program code:
#include <iostream>
using namespace std;
template <typename L1>
inline L1 rotateCarryRightmost( L1 operateMe, unsigned char numL1erShift )
{
static const unsigned char L1CountL1it =sizeof( L1 )*8U ;
return ( operateMe>> numL1erShift ) | ( operateMe<< ( L1CountL1it-numL1erShift ) ) ;
}
template <typename L1>
inline L1 rotateCarryLeftmost( L1 operateMe, unsigned char numL1erShift )
{
static const unsigned char L1CountL1it=sizeof( L1 )*8U ;
return ( operateMe<<numL1erShift ) | (operateMe>>( L1CountL1it - numL1erShift ) ) ;
}
void main()
{
cout<<" unsigned-char" <<20U <<"rotated rightmost by-6 L1its== "<< +rotateCarryRightmost<unsigned char> ( 20U , 6U)<<" " ;
cout<<" unsigned-char" <<20U <<"rotated leftmost by-6 L1its== "<< +rotateCarryLeftmost<unsigned char> ( 20U , 6U)<<" " ;
cout<<" " ;
for( unsigned char numL1erShift= 0U ;numL1erShift <=sizeof( unsigned char )* 8U ;++numL1erShift )
{
cout<<" unsigned-char" <<21U <<"rotated rightmost by"<< +numL1erShift<<"L1it( s )== " <<+rotateCarryRightmost<unsigned char> ( 21U , numL1erShift )<<" " ;
}
cout <<" " ;
for( unsigned char numL1erShift= 0U ;numL1erShift <=sizeof( unsigned char )* 8U ;++numL1erShift )
{
cout<<" unsigned-char" <<21U <<" rotated leftmost by"<< +numL1erShift<<"bit( s )== " <<+rotateCarryLeftmost<unsigned char> ( 21U , numL1erShift )<<" " ;
}
cout<<" " ;
for( unsigned char numL1erShift= 0U ;numL1erShift <=sizeof( unsigned long long )*8U ; ++numL1erShift )
{
cout<<"unsigned long-long"<< 3457347ULL<<"rotated rightmost by" <<+numL1erShift <<"bit( s )== " << rotateCarryRightmost<unsigned long long> ( 3457347ULL , numL1erShift )<<" " ;
}
cout<< " ";
for( unsigned char numL1erShift= 0U ;numL1erShift <=sizeof( unsigned long long )*8U ; ++numL1erShift )
{
cout<<"unsigned long-long" << 3457347ULL<<"rotated leftmost by" <<+numL1erShift <<"bit( s )== " << rotateCarryLeftmost<unsigned long long> ( 3457347ULL , numL1erShift)<<" " ;
}
cout<<" " ;
system( " pause " ) ;
}
Output of the program code:
unsigned-char20rotated rightmost by-6 bits== 80
unsigned-char20rotated leftmost by-6 bits== 5
unsigned-char21rotated rightmost by0L1it( s )== 21
unsigned-char21rotated rightmost by1L1it( s )== 138
unsigned-char21rotated rightmost by2L1it( s )== 69
unsigned-char21rotated rightmost by3L1it( s )== 162
unsigned-char21rotated rightmost by4L1it( s )== 81
unsigned-char21rotated rightmost by5L1it( s )== 168
unsigned-char21rotated rightmost by6L1it( s )== 84
unsigned-char21rotated rightmost by7L1it( s )== 42
unsigned-char21rotated rightmost by8L1it( s )== 21
unsigned-char21 rotated leftmost by0bit( s )== 21
unsigned-char21 rotated leftmost by1bit( s )== 42
unsigned-char21 rotated leftmost by2bit( s )== 84
unsigned-char21 rotated leftmost by3bit( s )== 168
unsigned-char21 rotated leftmost by4bit( s )== 81
unsigned-char21 rotated leftmost by5bit( s )== 162
unsigned-char21 rotated leftmost by6bit( s )== 69
unsigned-char21 rotated leftmost by7bit( s )== 138
unsigned-char21 rotated leftmost by8bit( s )== 21
unsigned long-long3457347rotated rightmost by0bit( s )== 3457347
unsigned long-long3457347rotated rightmost by1bit( s )== 9223372036856504481
unsigned long-long3457347rotated rightmost by2bit( s )== 13835058055283028048
unsigned long-long3457347rotated rightmost by3bit( s )== 6917529027641514024
unsigned long-long3457347rotated rightmost by4bit( s )== 3458764513820757012
unsigned long-long3457347rotated rightmost by5bit( s )== 1729382256910378506
unsigned long-long3457347rotated rightmost by6bit( s )== 864691128455189253
unsigned long-long3457347rotated rightmost by7bit( s )== 9655717601082370434
unsigned long-long3457347rotated rightmost by8bit( s )== 4827858800541185217
unsigned long-long3457347rotated rightmost by9bit( s )== 11637301437125368416
unsigned long-long3457347rotated rightmost by10bit( s )== 5818650718562684208
unsigned long-long3457347rotated rightmost by11bit( s )== 2909325359281342104
unsigned long-long3457347rotated rightmost by12bit( s )== 1454662679640671052
unsigned long-long3457347rotated rightmost by13bit( s )== 727331339820335526
unsigned long-long3457347rotated rightmost by14bit( s )== 363665669910167763
unsigned long-long3457347rotated rightmost by15bit( s )== 9405204871809859689
unsigned long-long3457347rotated rightmost by16bit( s )== 13925974472759705652
unsigned long-long3457347rotated rightmost by17bit( s )== 6962987236379852826
unsigned long-long3457347rotated rightmost by18bit( s )== 3481493618189926413
unsigned long-long3457347rotated rightmost by19bit( s )== 10964118845949739014
unsigned long-long3457347rotated rightmost by20bit( s )== 5482059422974869507
unsigned long-long3457347rotated rightmost by21bit( s )== 11964401748342210561
unsigned long-long3457347rotated rightmost by22bit( s )== 15205572911025881088
unsigned long-long3457347rotated rightmost by23bit( s )== 7602786455512940544
unsigned long-long3457347rotated rightmost by24bit( s )== 3801393227756470272
unsigned long-long3457347rotated rightmost by25bit( s )== 1900696613878235136
unsigned long-long3457347rotated rightmost by26bit( s )== 950348306939117568
unsigned long-long3457347rotated rightmost by27bit( s )== 475174153469558784
unsigned long-long3457347rotated rightmost by28bit( s )== 237587076734779392
unsigned long-long3457347rotated rightmost by29bit( s )== 118793538367389696
unsigned long-long3457347rotated rightmost by30bit( s )== 59396769183694848
unsigned long-long3457347rotated rightmost by31bit( s )== 29698384591847424
unsigned long-long3457347rotated rightmost by32bit( s )== 14849192295923712
unsigned long-long3457347rotated rightmost by33bit( s )== 7424596147961856
unsigned long-long3457347rotated rightmost by34bit( s )== 3712298073980928
unsigned long-long3457347rotated rightmost by35bit( s )== 1856149036990464
unsigned long-long3457347rotated rightmost by36bit( s )== 928074518495232
unsigned long-long3457347rotated rightmost by37bit( s )== 464037259247616
unsigned long-long3457347rotated rightmost by38bit( s )== 232018629623808
unsigned long-long3457347rotated rightmost by39bit( s )== 116009314811904
unsigned long-long3457347rotated rightmost by40bit( s )== 58004657405952
unsigned long-long3457347rotated rightmost by41bit( s )== 29002328702976
unsigned long-long3457347rotated rightmost by42bit( s )== 14501164351488
unsigned long-long3457347rotated rightmost by43bit( s )== 7250582175744
unsigned long-long3457347rotated rightmost by44bit( s )== 3625291087872
unsigned long-long3457347rotated rightmost by45bit( s )== 1812645543936
unsigned long-long3457347rotated rightmost by46bit( s )== 906322771968
unsigned long-long3457347rotated rightmost by47bit( s )== 453161385984
unsigned long-long3457347rotated rightmost by48bit( s )== 226580692992
unsigned long-long3457347rotated rightmost by49bit( s )== 113290346496
unsigned long-long3457347rotated rightmost by50bit( s )== 56645173248
unsigned long-long3457347rotated rightmost by51bit( s )== 28322586624
unsigned long-long3457347rotated rightmost by52bit( s )== 14161293312
unsigned long-long3457347rotated rightmost by53bit( s )== 7080646656
unsigned long-long3457347rotated rightmost by54bit( s )== 3540323328
unsigned long-long3457347rotated rightmost by55bit( s )== 1770161664
unsigned long-long3457347rotated rightmost by56bit( s )== 885080832
unsigned long-long3457347rotated rightmost by57bit( s )== 442540416
unsigned long-long3457347rotated rightmost by58bit( s )== 221270208
unsigned long-long3457347rotated rightmost by59bit( s )== 110635104
unsigned long-long3457347rotated rightmost by60bit( s )== 55317552
unsigned long-long3457347rotated rightmost by61bit( s )== 27658776
unsigned long-long3457347rotated rightmost by62bit( s )== 13829388
unsigned long-long3457347rotated rightmost by63bit( s )== 6914694
unsigned long-long3457347rotated rightmost by64bit( s )== 3457347
unsigned long-long3457347rotated leftmost by0bit( s )== 3457347
unsigned long-long3457347rotated leftmost by1bit( s )== 6914694
unsigned long-long3457347rotated leftmost by2bit( s )== 13829388
unsigned long-long3457347rotated leftmost by3bit( s )== 27658776
unsigned long-long3457347rotated leftmost by4bit( s )== 55317552
unsigned long-long3457347rotated leftmost by5bit( s )== 110635104
unsigned long-long3457347rotated leftmost by6bit( s )== 221270208
unsigned long-long3457347rotated leftmost by7bit( s )== 442540416
unsigned long-long3457347rotated leftmost by8bit( s )== 885080832
unsigned long-long3457347rotated leftmost by9bit( s )== 1770161664
unsigned long-long3457347rotated leftmost by10bit( s )== 3540323328
unsigned long-long3457347rotated leftmost by11bit( s )== 7080646656
unsigned long-long3457347rotated leftmost by12bit( s )== 14161293312
unsigned long-long3457347rotated leftmost by13bit( s )== 28322586624
unsigned long-long3457347rotated leftmost by14bit( s )== 56645173248
unsigned long-long3457347rotated leftmost by15bit( s )== 113290346496
unsigned long-long3457347rotated leftmost by16bit( s )== 226580692992
unsigned long-long3457347rotated leftmost by17bit( s )== 453161385984
unsigned long-long3457347rotated leftmost by18bit( s )== 906322771968
unsigned long-long3457347rotated leftmost by19bit( s )== 1812645543936
unsigned long-long3457347rotated leftmost by20bit( s )== 3625291087872
unsigned long-long3457347rotated leftmost by21bit( s )== 7250582175744
unsigned long-long3457347rotated leftmost by22bit( s )== 14501164351488
unsigned long-long3457347rotated leftmost by23bit( s )== 29002328702976
unsigned long-long3457347rotated leftmost by24bit( s )== 58004657405952
unsigned long-long3457347rotated leftmost by25bit( s )== 116009314811904
unsigned long-long3457347rotated leftmost by26bit( s )== 232018629623808
unsigned long-long3457347rotated leftmost by27bit( s )== 464037259247616
unsigned long-long3457347rotated leftmost by28bit( s )== 928074518495232
unsigned long-long3457347rotated leftmost by29bit( s )== 1856149036990464
unsigned long-long3457347rotated leftmost by30bit( s )== 3712298073980928
unsigned long-long3457347rotated leftmost by31bit( s )== 7424596147961856
unsigned long-long3457347rotated leftmost by32bit( s )== 14849192295923712
unsigned long-long3457347rotated leftmost by33bit( s )== 29698384591847424
unsigned long-long3457347rotated leftmost by34bit( s )== 59396769183694848
unsigned long-long3457347rotated leftmost by35bit( s )== 118793538367389696
unsigned long-long3457347rotated leftmost by36bit( s )== 237587076734779392
unsigned long-long3457347rotated leftmost by37bit( s )== 475174153469558784
unsigned long-long3457347rotated leftmost by38bit( s )== 950348306939117568
unsigned long-long3457347rotated leftmost by39bit( s )== 1900696613878235136
unsigned long-long3457347rotated leftmost by40bit( s )== 3801393227756470272
unsigned long-long3457347rotated leftmost by41bit( s )== 7602786455512940544
unsigned long-long3457347rotated leftmost by42bit( s )== 15205572911025881088
unsigned long-long3457347rotated leftmost by43bit( s )== 11964401748342210561
unsigned long-long3457347rotated leftmost by44bit( s )== 5482059422974869507
unsigned long-long3457347rotated leftmost by45bit( s )== 10964118845949739014
unsigned long-long3457347rotated leftmost by46bit( s )== 3481493618189926413
unsigned long-long3457347rotated leftmost by47bit( s )== 6962987236379852826
unsigned long-long3457347rotated leftmost by48bit( s )== 13925974472759705652
unsigned long-long3457347rotated leftmost by49bit( s )== 9405204871809859689
unsigned long-long3457347rotated leftmost by50bit( s )== 363665669910167763
unsigned long-long3457347rotated leftmost by51bit( s )== 727331339820335526
unsigned long-long3457347rotated leftmost by52bit( s )== 1454662679640671052
unsigned long-long3457347rotated leftmost by53bit( s )== 2909325359281342104
unsigned long-long3457347rotated leftmost by54bit( s )== 5818650718562684208
unsigned long-long3457347rotated leftmost by55bit( s )== 11637301437125368416
unsigned long-long3457347rotated leftmost by56bit( s )== 4827858800541185217
unsigned long-long3457347rotated leftmost by57bit( s )== 9655717601082370434
unsigned long-long3457347rotated leftmost by58bit( s )== 864691128455189253
unsigned long-long3457347rotated leftmost by59bit( s )== 1729382256910378506
unsigned long-long3457347rotated leftmost by60bit( s )== 3458764513820757012
unsigned long-long3457347rotated leftmost by61bit( s )== 6917529027641514024
unsigned long-long3457347rotated leftmost by62bit( s )== 13835058055283028048
unsigned long-long3457347rotated leftmost by63bit( s )== 9223372036856504481
unsigned long-long3457347rotated leftmost by64bit( s )== 3457347
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.