For a FFT function I need to permutate or shuffle the elements within an array in a bit-reversed way. That\'s a common task with FFTs because most power of two sized FFT fun
If you think about what's happening to the bitswapped index, it's being counted up in the same way that the non-bitswapped index is being counted up, just with the bits being used in the reverse order from conventional counting.
Rather than bitswapping the index every time through the loop you can manually implement a '++' equivalent that uses bits in the wrong order to do a double indexed for loop. I've verified that gcc at O3 inlines the increment function, but as to whether it's any faster then bitswapping the number via a lookup every time, that's for the profiler to say.
Here's an illustrative test program.
#include
void RevBitIncr( int *n, int bit )
{
do
{
bit >>= 1;
*n ^= bit;
} while( (*n & bit) == 0 && bit != 1 );
}
int main(void)
{
int max = 0x100;
int i, j;
for( i = 0, j = 0; i != max; ++i, RevBitIncr( &j, max ) )
{
if( i < j )
printf( "%02x <-> %02x\n", i, j );
}
return 0;
}