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

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

    I know it isn't C but asm:

    var1 dw 0f0f0
    clc
         push ax
         push cx
         mov cx 16
    loop1:
         shl var1
         shr ax
    loop loop1
         pop ax
         pop cx
    

    This works with the carry bit, so you may save flags too

提交回复
热议问题