Question

In: Computer Science

C++ Circular Shift - bits fall off at one end are put back to the other...

C++

Circular Shift - bits fall off at one end are put back to the other end. In left rotation, the bits that fall off at left end are put back at right end. In right rotation, the bits that fall off at right end are put back at left end. Let n stored using 8 bits:

Left rotation of n = 11100101 by 3 makes n = 00101111
Right rotation of n = 11100101 by 3 makes n = 10111100

Use functions below;

int leftrotate(int n, unsigned int d){

}

int rightrotate(int n, unsigned int d){

}

Solutions

Expert Solution

The program can be found below

// C++ code to rotate bits of number
#include<iostream>

using namespace std;
#define INT_BITS 32
class circularRotate
{

/*Function to left rotate n by d bits*/
public:
int leftRotate(int n, unsigned int d)
{

    /* In n<<d, last d bits are 0. To
     put first 3 bits of n at
    last, do bitwise or of n<<d
    with n >>(INT_BITS - d) */
    return (n << d)|(n >> (INT_BITS - d));
}

/*Function to right rotate n by d bits*/
int rightRotate(int n, unsigned int d)
{
    /* In n>>d, first d bits are 0.
    To put last 3 bits of at
    first, do bitwise or of n>>d
    with n <<(INT_BITS - d) */
    return (n >> d)|(n << (INT_BITS - d));
}
};

/* Main code*/
int main()
{
    circularRotate g;
    int n = 16;
    int d = 2;
    cout << "Left Rotation of " << n <<
            " by " << d << " is ";
    cout << g.leftRotate(n, d);
    cout << "\nRight Rotation of " << n <<
            " by " << d << " is ";
    cout << g.rightRotate(n, d);
    getchar();
}

Related Solutions

A circular Euler column with one end fixed and the other pinned (or hinged or simply-supported)...
A circular Euler column with one end fixed and the other pinned (or hinged or simply-supported) is under a compressive axial load. Given the length L of the column and bending rigidity EI, what is the buckling load Pcr ?
A pipe of diameter 350mm at one end and the diameter at the other end is...
A pipe of diameter 350mm at one end and the diameter at the other end is 200mm. The datum head is same for both ends. Calculate (i) Intensity of pressure at the smaller end if the pressure at the bigger end is 40.5 N/cm2. (ii) Determine the head loss between smaller end and bigger end. Take discharge through the pipe as 40 litres/sec. (iii) Scope for this obtained results of head loss between both ends the like that only!
A pipe of diameter 350mm at one end and the diameter at the other end is...
A pipe of diameter 350mm at one end and the diameter at the other end is 200mm. The datum head is same for both ends. Calculate (i) Intensity of pressure at the smaller end if the pressure at the bigger end is 40.5 N/cm2. (ii) Determine the head loss between smaller end and bigger end. Take discharge through the pipe as 40 litres/sec. (iii) Scope for this obtained results of head loss between both ends.
Can you please provide me end off shift report(handoff report)and nursing concern to this senario and...
Can you please provide me end off shift report(handoff report)and nursing concern to this senario and nursing action the nurse has been working a 12 hour day shift providing care for Mr. O, a 62 year old male who has cellulitis of the right leg. He has a past medical history of angina, hypertension, and DM Type 2. During Morning Report, you are told: • Mr. O stays in a chair most of the time with his leg elevated •...
Can you please provide me end off shift report(handoff report)and nursing concern to this senario and...
Can you please provide me end off shift report(handoff report)and nursing concern to this senario and nursing action the nurse has been working a 12 hour day shift providing care for Mr. O, a 62 year old male who has cellulitis of the right leg. He has a past medical history of angina, hypertension, and DM Type 2. During Morning Report, you are told: • Mr. O stays in a chair most of the time with his leg elevated •...
A ball is attached to one end of a wire, the other end being fastened to...
A ball is attached to one end of a wire, the other end being fastened to the ceiling. The wire is held horizontal, and the ball is released from rest (see the drawing). It swings downward and strikes a block initially at rest on a horizontal frictionless surface. Air resistance is negligible, and the collision is elastic. The masses of the ball and block are, respectively, 1.6 kg and 2.3 kg, and the length of the wire is 1.21 m....
A cantilever of circular solid cross section is fixed at one end and carries a concentrated...
A cantilever of circular solid cross section is fixed at one end and carries a concentrated load P at the free end. The diameter at the free end is 200mm and increases uniformly to 400 mm at the fixed end over a length of 2m. At what distance from the free will the bending stresses in the cantilever be maximum? Also calculate the value of the maximum bending stress if the concentrated load P=300 KN (a) 1.23 m, 12.32 MPa...
One end of a very long wire is rigidly fastened to a post. The other end...
One end of a very long wire is rigidly fastened to a post. The other end is attached to an audio speaker which shakes the end of the wire in a direction perpendicular to its length. The wire has a diameter 1.1 mm , a volume mass density of 7800 kg/m3, and is held under a tension of 6.7 N. The speaker creates a wave on the wire with an amplitude 0.56 cm and a frequency of 65.0 Hz ....
one mole of ice at 0 C is put into contact with one mole of steam...
one mole of ice at 0 C is put into contact with one mole of steam at 100 C in an adiabatic enclosure. The pressure is held constant at one atmosphere. What is the final composition and temperature of the system?
A horizontal wire has one end fastened to a wall while the other end is pulled...
A horizontal wire has one end fastened to a wall while the other end is pulled with a force of 200 N. The wire has a length of 5 m, a volume mass density of 7,400 kg/m3 , and a resistivity of 6.9e-07 Ωm. A transverse pulse takes 0.2 s to travel the length of the wire. Calculate the electrical power dissipated by the wire while a current of 2 A is passing through the wire
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT