How do I rotate the bits of given number, input number of rotations, at runtime?
For example:
binary: 10000000000000000000000000001011
rotations: 3 times right
result: 01110000000000000000000000000001
Similarly:
binary 10000000000000000000000000001011
rotations: 4 times left
result: 00000000000000000000000010111000
Below is my code to swap, but I couldn't find the logic to rotate the bits.
#include<stdio.h>
int main()
{
int num,bitleft,bitright,i,j;
printf("enter ur number\n");
scanf("%d",&num);
for(i=31,j=0;i>j;i--,j++)
{
bitleft=num>>i&1; //storing bits in integer from left
bitright=num>>j&1; //storing bits in integer from right
if(bitleft!=bitright) // checking if bits are not similarly
{
num=num^1<<i; // not similar then complement
num=num^1<<j;
}
}
printf("\n");
for(j=31;j>=0;j--) // loop to print swapped bits
{
if(num&1<<j)
printf("1");
else
printf("0");
}
}