write a C function to reverse the bits of a Byte as example if we have variable unsigned char x=0x07=0b00000111 as input parameter after reversing its bits it would return 11100000
solution
This file contains 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> | |
unsigned char reverse (unsigned char inbyte) | |
{ | |
unsigned char outbyte=0; | |
for(int i=0;i<=7;i++) | |
{ | |
if(inbyte&(1<<i)) | |
{ | |
outbyte|=1<<(7-i); | |
} | |
else | |
{ | |
continue; | |
} | |
} | |
return outbyte; | |
} | |
int main(int argc, char** argv) { | |
unsigned char x=1; | |
unsigned char revx=reverse(x); | |
printf("%d\n",revx); | |
return (EXIT_SUCCESS); | |
} |
No comments:
Post a Comment