Program Discussion :: Basics
107 / 279
lets 32bit no. write a program to circular rotate 1 byte.
int y=0x01020304;
output y=0x04010203;
Answer:
#include
#include
using namespace std;
#define INT_BITS 32
int leftRotate(int n, unsigned int d)
{
return (n > (INT_BITS - d));
}
int rightRotate(int n, unsigned int d)
{
return (n >> d)|(n
Asked In ::
Language:
Neha
7 Jul, 2017 9:30 AM
#include<stdio.h>
#define INT_BITS 32
/*Function to left rotate n by d bits*/
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));
}
/* Driver program to test above functions */
int main()
{
int n = 16;
int d = 2;
printf("Left Rotation of %d by %d is ", n, d);
printf("%d", leftRotate(n, d));
printf("\nRight Rotation of %d by %d is ", n, d);
printf("%d", rightRotate(n, d));
getchar();
}
Language:
Aarkin
7 Jul, 2017 9:30 AM
#include<stdio.h>
#include<iostream>
using namespace std;
#define INT_BITS 32
int leftRotate(int n, unsigned int d)
{
return (n << d)|(n >> (INT_BITS - d));
}
int rightRotate(int n, unsigned int d)
{
return (n >> d)|(n << (INT_BITS - d));
}
/* Driver program to test above functions */
int main()
{
int n = 16;
int d = 2;
cout<<"Left Rotation of" <<n<< "by "<<d<<" is ";
cout<<leftRotate(n, d);
printf("\nRight Rotation of"<<n<< "by "<<d<<" is ";
cout<< rightRotate(n, d);
getchar();
}