Possible Duplicate:
Best Algorithm for Bit Reversal ( from MSB->LSB to LSB->MSB) in C
I have a 64-bit word and I want to do the following operations on it.
First I want to do a bit-swap (swap Bit 63 with Bit 0
swap Bit 62 with Bit 1 and so on)
Once the above operation is completed I want to do a byte-swap Swap between Byte 0 and Byte 7
Byte 1 and Byte 6 and so on.
Now we do have an inbuilt function in gcc linux to do the second part bswap_64().Is there any function do do the first part available in gcc linux C
The net effect is the same as bit-swapping each byte in place. For example, byte 0 is first copied to byte 7 with its bits reversed, then copied back to byte0 with no bit-reversal.
There’s no built-in support for any of these operations, but bit-swapping each byte in place should be fairly straightforward. The most efficient method is probably a 256-element lookup table.
where:
You can write a small program that computes the bit-swapped value for each byte value and generates the source code for the initialization of
bytes. (Or, since you’re writing the code to do the computation anyway, you can just use it in your program in place of the lookup table; it depends on how important speed is.)This assumes, for example, that
CHAR_BIT == 8, which is not guaranteed by the language.I have not tested this.