Two suggestions:
Instead of using the logic operations, you can use a look up table of 256 chars.:
char lookUpTable[256] = {0x00, 0x02, 0x01 ...};
If you dont want to initialize this lookup statically, you can write a function that initialize it using logic operations.
When you want to swap byte b, you either simply write lookUpTable[b], or you wrap this with a function.
As for swapping any type, you write a function that does something like:
void SwapBits(char* data, size_t len)
{
For (; len > 0; len--)
{
*data = lookUpTable[*data];
data++;
}
}
You then use this like this:
AnyType swapMe = whatever;
SwapBits((char*)(&swapMe), sizeof(AnyType));
Note this replaces the "contents" of swapMe.
One more thing, the right shift behavior is architecrure specific, some architectures may sign extend on right shift. It would be more generic to use an expression like:
SwappedByte = ((Byte >> 1)&0x55) | ((Byte << 1)&0xaa)
As this way you remove any sign extension artifacts.