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

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

    How about the following:

        uint reverseMSBToLSB32ui(uint input)
        {
            uint output = 0x00000000;
            uint toANDVar = 0;
            int places = 0;
    
            for (int i = 1; i < 32; i++)
            {
                places = (32 - i);
                toANDVar = (uint)(1 << places);
                output |= (uint)(input & (toANDVar)) >> places;
    
            }
    
    
            return output;
        }
    

    Small and easy (though, 32 bit only).

提交回复
热议问题