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

后端 未结 26 1668
情深已故
情深已故 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:09

    unsigned char ReverseBits(unsigned char data)
    {
        unsigned char k = 0, rev = 0;
    
        unsigned char n = data;
    
        while(n)
    
        {
            k = n & (~(n - 1));
            n &= (n - 1);
            rev |= (128 / k);
        }
        return rev;
    }
    

提交回复
热议问题