P
Paul Morrison
Hi
I have found a function that outputs an 8 bit value in reverse order, but am
not really sure how it works. Would anyone please be kind enough to explain
it?
/* Return an unsigned char that contains the bit pattern in c
* in the reverse order. For example, if c is 01101011 then
* the value returned should be 11010110. Assume that a char
* variable is always 8 bits.
*/
unsigned char reverse_bits(unsigned char c)
{
unsigned char y = 0;
y += (x & 0x80) >> 7;
y += (x & 0x40) >> 5;
y += (x & 0x20) >> 3;
y += (x & 0x10) >> 1;
y += (x & 0x01) << 7;
y += (x & 0x02) << 5;
y += (x & 0x04) << 3;
y += (x & 0x08) << 1;
return y;
}
Thanks for your help.
I have found a function that outputs an 8 bit value in reverse order, but am
not really sure how it works. Would anyone please be kind enough to explain
it?
/* Return an unsigned char that contains the bit pattern in c
* in the reverse order. For example, if c is 01101011 then
* the value returned should be 11010110. Assume that a char
* variable is always 8 bits.
*/
unsigned char reverse_bits(unsigned char c)
{
unsigned char y = 0;
y += (x & 0x80) >> 7;
y += (x & 0x40) >> 5;
y += (x & 0x20) >> 3;
y += (x & 0x10) >> 1;
y += (x & 0x01) << 7;
y += (x & 0x02) << 5;
y += (x & 0x04) << 3;
y += (x & 0x08) << 1;
return y;
}
Thanks for your help.