Trusted by millions of Kenyans
Study resources on Kenyaplex

Get ready-made curriculum aligned revision materials

Exam papers, notes, holiday assignments and topical questions – all aligned to the Kenyan curriculum.

Create an overload rotate() function using C++ new style that left-rotates the bits in its argument and returns the result.Overload it so it accepts ints...

Create an overload rotate() function using C++ new style that left-rotates the bits in its argument and returns the result.Overload it so it accepts ints and longs.(A rotate is similar to a shift except that the bit shifted off one end is shifted onto the other end)

Answers


Davis
#include
using namespace std;
int rotate (int i);
long rotate (long i);
int main ()
{
int a;
long b;
a = 0x8000;
b = 8;
cout << rotate (a);
cout << "\n";
cout << rotate (b);
return 0;
}
int rotate (int i)
{
int x;
if (i & 0x8000) x = 1;
else x = 0;
i = i << 1;
i += x;
return i;
}
long rotate(long i)
{
int x;
if ( i & 0x80000000) x = 1;
else x = 0;
i = i << 1;
i += x;
return i;
}
Githiari answered the question on May 5, 2018 at 17:43

Answer Attachments

Exams With Marking Schemes

Related Questions