I'm doing circular right shift and left shift in C, I'm wrong somewhere. For right rotation if I give the input as 123 and number of rotations as 3, the output what I get is wrong. Help me to find the mistake please.
#include<stdio.h>
#include <stdlib.h>
void rotateLeft(unsigned long int num,int n);
void rotateRight(unsigned long int num,int n);
void bin_print(unsigned long int num);
int main()
{
printf("\tThis program is to circular right & left shift the int number by n\n\n");
printf("Possible operations\n1. circular right shift\n2. circular left shift\n");
int choice,n;
unsigned long int num;
printf("Enter your choice: ");
scanf("%d",&choice);
printf("Enter a number: ");
scanf("%lu", &num);
bin_print(num);
printf("Enter number of rotation: ");
scanf("%d", &n);
(choice==1) ? rotateRight(num,n) : rotateLeft(num,n);
}
void bin_print(unsigned long int num)
{
for(int i = 31; i >= 0; i--)
{
if((num & (1 << i))) {
printf("%d",1); // The ith digit is one
}
else {
printf("%d",0); // The ith digit is zero
}
if(i%8==0) printf(" ");
}
printf("\n");
}
void rotateLeft(unsigned long int num, int n)
{
unsigned long int val = (num << n) | (num >> (32 - n));
bin_print(val);
printf("%ld",val);
}
void rotateRight(unsigned long int num,int n)
{
unsigned long int val = (num >> n) | (num << (32 - n));
bin_print(val);
printf("%ld",val);
}