Efficient Algorithm for Bit Reversal (from MSB->LSB to LSB->MSB) in C

后端 未结 26 1371
情深已故
情深已故 2020-11-22 06:08

What is the most efficient algorithm to achieve the following:

0010 0000 => 0000 0100

The conversion is from MSB->LSB to LSB->MSB. All bits

相关标签:
26条回答
  • 2020-11-22 07:11

    My simple solution

    BitReverse(IN)
        OUT = 0x00;
        R = 1;      // Right mask   ...0000.0001
        L = 0;      // Left mask    1000.0000...
        L = ~0; 
        L = ~(i >> 1);
        int size = sizeof(IN) * 4;  // bit size
    
        while(size--){
            if(IN & L) OUT = OUT | R; // start from MSB  1000.xxxx
            if(IN & R) OUT = OUT | L; // start from LSB  xxxx.0001
            L = L >> 1;
            R = R << 1; 
        }
        return OUT;
    
    0 讨论(0)
  • 2020-11-22 07:12

    Well, this is basically the same as the first "reverse()" but it is 64 bit and only needs one immediate mask to be loaded from the instruction stream. GCC creates code without jumps, so this should be pretty fast.

    #include <stdio.h>
    
    static unsigned long long swap64(unsigned long long val)
    {
    #define ZZZZ(x,s,m) (((x) >>(s)) & (m)) | (((x) & (m))<<(s));
    /* val = (((val) >>16) & 0xFFFF0000FFFF) | (((val) & 0xFFFF0000FFFF)<<16); */
    
    val = ZZZZ(val,32,  0x00000000FFFFFFFFull );
    val = ZZZZ(val,16,  0x0000FFFF0000FFFFull );
    val = ZZZZ(val,8,   0x00FF00FF00FF00FFull );
    val = ZZZZ(val,4,   0x0F0F0F0F0F0F0F0Full );
    val = ZZZZ(val,2,   0x3333333333333333ull );
    val = ZZZZ(val,1,   0x5555555555555555ull );
    
    return val;
    #undef ZZZZ
    }
    
    int main(void)
    {
    unsigned long long val, aaaa[16] =
     { 0xfedcba9876543210,0xedcba9876543210f,0xdcba9876543210fe,0xcba9876543210fed
     , 0xba9876543210fedc,0xa9876543210fedcb,0x9876543210fedcba,0x876543210fedcba9
     , 0x76543210fedcba98,0x6543210fedcba987,0x543210fedcba9876,0x43210fedcba98765
     , 0x3210fedcba987654,0x210fedcba9876543,0x10fedcba98765432,0x0fedcba987654321
     };
    unsigned iii;
    
    for (iii=0; iii < 16; iii++) {
        val = swap64 (aaaa[iii]);
        printf("A[]=%016llX Sw=%016llx\n", aaaa[iii], val);
        }
    return 0;
    }
    
    0 讨论(0)
提交回复
热议问题