Skip to content

Instantly share code, notes, and snippets.

@liuml07
Created December 2, 2013 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuml07/7747040 to your computer and use it in GitHub Desktop.
Save liuml07/7747040 to your computer and use it in GitHub Desktop.
Reverse bits in word by lookup table
static const unsigned char BitReverseTable256[256] =
{
# define R2(n) n, n + 2*64, n + 1*64, n + 3*64
# define R4(n) R2(n), R2(n + 2*16), R2(n + 1*16), R2(n + 3*16)
# define R6(n) R4(n), R4(n + 2*4 ), R4(n + 1*4 ), R4(n + 3*4 )
R6(0), R6(2), R6(1), R6(3)
};
// reverse 32-bit value, 8 bits at time
unsigned int reverse_int(unsigned int v) {
return (BitReverseTable256[v & 0xff] << 24) |
(BitReverseTable256[(v >> 8) & 0xff] << 16) |
(BitReverseTable256[(v >> 16) & 0xff] << 8) |
(BitReverseTable256[(v >> 24) & 0xff]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment