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
note: data is 1 byte variable
refer to this video to know more about circular bit shifts
Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
No comments:
Post a Comment