views:

295

answers:

4

Possible Duplicate:
Best Algorithm for Bit Reversal ( from MSB->LSB to LSB->MSB) in C

Hi All, I had just written down a bit reversal function and its shown below.I feel its lengthy.So is there any faster and short method or any other alternative which you feel is much more faster and better.

Assuming integer is 4 bytes:

        int BitReversal(unsigned int);
        main()
        {
          unsigned int a;
          printf("Enter any unsigned number:\n");
          scanf("%d",&a);
          BitReversal(a);
          getch();
        }
       int BitReversal(unsigned int Num)
       {
          int i=0;
          int Temp = Num;
          unsigned int Res= 0;
          int count = 31;


         while(i<32)
         {
           Num = (Num>>i)&0x01;
           Num = Num<<count;
           Res|=Num;
           count--;
           i++;
           Num = Temp;
        }
         printf("The num is %x",Res);

}

 Thanks a lot
 Maddy
+1  A: 

Use the BSWAP opcode (or an equivalent function) to swap bytes, and use a lookup table (an array of 256 byte values) to swap the bits within each byte.

ChrisW
`bswap32` can be found in `#include <sys/endian.h>` on BSD and `#include <endian.h>` on Linux. GCC also has a `__builtin_bswap32` intrinsic.
ephemient
A: 

You can take a look here: BitReversal

Naveen
A: 

check these lovely hacks

dfa