AD1

Monday, 13 March 2017

Reverse bits of Byte

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 


#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);
}
view raw rev.c hosted with ❤ by GitHub

No comments:

Post a Comment