Possible Duplicate:
C reverse bits in unsigned integer
How can I reverse a binary number only using binary operators?
E.g:
11100000 -> 00000111
00110100 -> 00101100
00111111 -> 11111100
Possible Duplicate:
C reverse bits in unsigned integer
How can I reverse a binary number only using binary operators?
E.g:
11100000 -> 00000111
00110100 -> 00101100
00111111 -> 11111100
You can see this website http://graphics.stanford.edu/~seander/bithacks.html
Reversing bit sequences
Reverse bits the obvious way
Reverse bits in word by lookup table
Reverse the bits in a byte with 3 operations (64-bit multiply and modulus division)
Reverse the bits in a byte with 4 operations (64-bit multiply, no division)
Reverse the bits in a byte with 7 operations (no 64-bit, only 32)
Reverse an N-bit quantity in parallel with 5 * lg(N) operations
For this sort of thing I recommend that you take a look at the awesome page Bit Twiddling Hacks.
Here is just one example solution taken from that page:
And as pointed out in the comments, here's another option: