AD1

Saturday, 17 June 2017

Circular Bit Shift

Write two functions RL(data,n) which performs circular left shift on variable data by n bits , RR(data,n) which performs circular right shift on variable data by n bits
note: data is 1 byte variable

refer to this video to know more about circular bit shifts




Solution
#include <stdio.h>
#include <stdlib.h>
char RL(unsigned char byte,unsigned int n)
{
unsigned char temp=0;
for(int x=0;x<n;x++)
{
temp=byte&(1<<7);//save last bit
byte=byte<<1;//shift n left by 1 bit
byte|=temp;//save last bit in first position from right
}
return byte;
}
char RR(unsigned char byte,unsigned int n)
{
unsigned char temp=0;
for(int x=0;x<n;x++)
{
temp=byte&(1<<0);//save first bit
byte=byte>>1;//shift n left by 1 bit
byte|=(temp<<7);//save first bit in last position from right
}
return byte;
}
int main(int argc, char** argv) {
printf("%d\n%d\n",RL(16,2),RR(16,2));
return (EXIT_SUCCESS);
}
view raw circularshift.c hosted with ❤ by GitHub

No comments:

Post a Comment