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

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

    This is for 32 bit, we need to change the size if we consider 8 bits.

        void bitReverse(int num)
        {
            int num_reverse = 0;
            int size = (sizeof(int)*8) -1;
            int i=0,j=0;
            for(i=0,j=size;i<=size,j>=0;i++,j--)
            {
                if((num >> i)&1)
                {
                    num_reverse = (num_reverse | (1<

    Reading the input integer "num" in LSB->MSB order and storing in num_reverse in MSB->LSB order.

提交回复
热议问题