Fast way to reverse the bits in a byte

This tip submitted by John Hite on 2013-06-24 23:37:38. It has been viewed 8877 times.
Rating of 6.6 with 34 votes

Sometimes speed is more important than space so if you have room for an array of 256 just put the reverse of the address in each location.

unsigned char reverse[255] =
0, 0x80, 0x40, 0xC0, 0x20, 0xA0, ...

When you get the backwards byte just use it as the subscript of this array to fetch its reverse.

Instead of creating arrays like this by hand I write a short prog in VC++ and then copy/paste the output window contents into my code.

More tips

Help your fellow programmers! Add a tip!