"John Deuf" <go****@spongers.com> wrote in message
news:97************************@posting.google.com ...
| Does somebody has a better algorithm than mine to reverse a byte
| (i.e. bit 0 becomes 7, bit 1 becomes 6 ...)
|
| unsigned char u=0, c = TESTVALUE;
| int i;
|
| for (i=0 ; i<4 ; i++) u |= ((c & (1 << i)) << (7-2*i))
| | ((c & (1 << 7-i)) >> (7-2*i));
As an alternative to a lookup, it is possible to perform
the operation in O(lg(N)) instead of O(N):
For an 8-bit byte stored in c:
c = ((c>>1)&0x55)|((c<<1)&0xAA);
c = ((c>>2)&0x33)|((c<<2)&0xCC);
c = (c>>4) | (c<<4) ; //NB: code not tested
This is obviously easy to scale up to 16/32/64 bytes and,
in some situations, could be faster than a lookup...
Regards,
Ivan
--
http://ivan.vecerina.com