c++cbit-manipulation

In C/C++ what's the simplest way to reverse the order of bits in a byte?


While there are multiple ways to reverse bit order in a byte, I'm curious as to what is the "simplest" for a developer to implement. And by reversing I mean:

1110 -> 0111
0010 -> 0100

This is similar to, but not a duplicate of this PHP question.

This is similar to, but not a duplicate of this C question. This question is asking for the easiest method to implement by a developer. The "Best Algorithm" is concerned with memory and cpu performance.


Solution

  • If you are talking about a single byte, a table-lookup is probably the best bet, unless for some reason you don't have 256 bytes available.