1

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

Community
  • 1
  • 1
liv2hak
  • 14,472
  • 53
  • 157
  • 270
  • 1
    No. But we do have these things called "operators" which do those operations... – Billy ONeal Nov 08 '11 at 22:54
  • **Why** do you want to swap bits? either FFT or homework, IMHO. – wildplasser Nov 08 '11 at 23:01
  • @wildplasser.it is neither FFT not homework.I have captured some idle 10GE frames which I am trying to convert to XGMII coded frames. :) – liv2hak Nov 08 '11 at 23:06
  • "Dupe" is a good answer, but it's not actually a duplicate of his question. – John Carter Nov 08 '11 at 23:08
  • @therefromhere: [this answer](http://stackoverflow.com/questions/746171/best-algorithm-for-bit-reversal-from-msb-lsb-to-lsb-msb-in-c/746203#746203) addresses perfectly the question. – Alexandre C. Nov 08 '11 at 23:12
  • @AlexandreC. I disagree. It's highly relevant, but it's not actually answering the question that was asked - "Is there a standard C function for bitswapping" (of course the answer should be No, with a link to that answer). – John Carter Nov 08 '11 at 23:40
  • Pity for the dup/closing. I had to append my solution to the older topic. – wildplasser Nov 09 '11 at 12:18

1 Answers1

7

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.

uint64_t the_word = /* whatever */
unsigned char *bytes = &the_word;
for (i = 0; i < 7; i ++) {
     bytes[i] = reverse[bytes[i]];
}

where:

const unsigned char reverse[UCHAR_MAX+1] {
    0x00, 0x80, ..., 0xFF
}

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.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • According to benchmarks [there](http://stackoverflow.com/questions/746171/best-algorithm-for-bit-reversal-from-msb-lsb-to-lsb-msb-in-c), it might be better to manually unroll the loop and use integer arithmetic, since compilers may not be *that* clever (and byte addressing is usually slow). Declaring `bytes` as `restrict` could also help. Benchmarking is of course your guide here. – Alexandre C. Nov 08 '11 at 23:27
  • @AlexandreC.: I just fixed the typo. – Keith Thompson Nov 09 '11 at 00:18