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

后端 未结 26 1355
情深已故
情深已故 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 06:59

    I thought this is one of the simplest way to reverse the bit. please let me know if there is any flaw in this logic. basically in this logic, we check the value of the bit in position. set the bit if value is 1 on reversed position.

    void bit_reverse(ui32 *data)
    {
      ui32 temp = 0;    
      ui32 i, bit_len;    
      {    
       for(i = 0, bit_len = 31; i <= bit_len; i++)   
       {    
        temp |= (*data & 1 << i)? (1 << bit_len-i) : 0;    
       }    
       *data = temp;    
      }    
      return;    
    }    
    

提交回复
热议问题