I'm trying to get my program to work, where I shift bits to the left and add the shifted bits to the right. For example 00111000
, if you shift it 4 positions to the left, the outcome should be 10000011
. How can I make this work, I know that I need to use the bitwise OR. I have added the main function below.
#include <stdio.h>
#include <stdlib.h>
void printbits(int b){
int i;
int s = 8 * (sizeof b) - 1; /* 31 if int is 32 bits */
for(i=s;i>=0;i--)
putchar( b & 1<<i ? '1' : '0');
}
int main(){
char dir; /* L=left R=right */
int val, n, i;
scanf("%d %d %c",&val, &n, &dir);
printbits(val);putchar('\n');
for (i=0; i<10; i++){
if (dir=='L' || dir =='l')
rotateLeft(&val, n);
else
rotateRight(&val,n);
printbits(val); putchar('\n');
}
return;
}
This is the rotateLeft en rotateRight function.
#include <stdio.h>
#include <stdlib.h>
void rotateLeft(int *val, int N){
int num = val[0];
int pos = N;
int result = num << pos;
}
void rotateRight(int *val, int N){
int num = val[0];
int pos = N;
int result = num >> pos;
}