In: Computer Science
C++: how to do a bit rotation
Left rotation of n = 11100101 by 3 makes n =
00101111 (Left shifted by 3 and first 3 bits are put back in
last).
Right rotation of n = 11100101 by 3 makes n = 10111100 (Right
shifted by 3 and last 3 bits are put back in first.
int left(int n, unsigned int d)
{
}
int right(int n, unsigned int d)
{
}
Full Program with the required functions:
#include<bits/stdc++.h>
using namespace std;
int left(int n, unsigned int d){
return (n << d)|(n >> (8 -
d));
}
int right(int n, unsigned int d){
return (n >> d)|(n << (8 -
d));
}
int main(){
cout<<left(16,2)<<endl;
cout<<right(16,2)<<endl;
}
Please refer to the screenshot of the code to understand the indentation of the code
Sample Run:
Hope this helped. Please do upvote and if there are any queries please ask in comments section.