-
Bit Reversal
How can I optimize the bit reversal code to reduce the number of iterations [code] #define NUMER_OF_BITS_IN_BYTE 8 #define SIZE_INT sizeof(int) #define SIZE_CHAR sizeof(char) #define MASK 0x1 #define CHAR int reversebits_int(int x) { int mask = MASK;; int rev = 0 ,pos = 0; while(mask) { rev |= (1
-
Code:
int reversebits_int(int x)
{
int rev;
rev = (x << 31) & 0x80000000;
rev |= (x << 29) & 0x40000000;
rev |= (x << 27) & 0x20000000;
...
rev |= (x << 1) & 0x00010000;
rev |= (x >> 1) & 0x00008000;
rev |= (x >> 3) & 0x00004000;
...
rev |= (x >> 31) & 0x00000001;
return rev;
}
This code has the minimal number of iterations possible - since there's no loop, there's 0 of them. Did you have a different question in mind?
-
Post the code and explain clearly what exactly you are trying to do with.
-
Screw that inefficient one-bit-at-a-time way of doing it!
Bit Twiddling Hacks - 'nuf said.