+ 1
How to reverse the 8-bits in byte by c Code?
I think it may be like circular shift left, but didn't work with me
4 Answers
+ 3
You forgot to store your shift of num back into it. Try this:
num >>=1;
+ 1
for(int i=8; i; i=i-1) {result = (result << 1) + (source & 1); source>>=1}
+ 1
right. Fixed.
0
I tried this, but it gives me reversed num= 255 not 128!, how to fix?
#include <stdio.h>
#include <stdlib.h>
void Reverse_8_bits(unsigned char num)
{
unsigned char i;
unsigned char result;
for(i=8; i>0 ; i--)
{
result = ((result << 1) + (num & 1));
num >>1;
}
printf("Reversed num= %hhu", result);
}
int main()
{
Reverse_8_bits(1);
return 0;
}