in-place bit-reversed shuffle on an array

前端 未结 7 1330
后悔当初
后悔当初 2020-12-05 08:50

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

相关标签:
7条回答
  • 2020-12-05 09:52

    A quick way to do this is to swap every adjacent single bit, then 2-bit fields, etc. The fast way to do this is:

    x = (x & 0x55) << 1 | (x & 0xAA) >> 1; //swaps bits
    x = (x & 0x33) << 2 | (x & 0xCC) >> 2; //swapss 2-bit fields
    x = (x & 0x0F) << 4 | (x & 0xF0) >> 4;
    

    While hard to read, if this is something that needs to be optimized you may want to do it this way.

    0 讨论(0)
提交回复
热议问题