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

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

    Bit reversal in pseudo code

    source -> byte to be reversed b00101100 destination -> reversed, also needs to be of unsigned type so sign bit is not propogated down

    copy into temp so original is unaffected, also needs to be of unsigned type so that sign bit is not shifted in automaticaly

    bytecopy = b0010110
    

    LOOP8: //do this 8 times test if bytecopy is < 0 (negative)

        set bit8 (msb) of reversed = reversed | b10000000 
    
    else do not set bit8
    
    shift bytecopy left 1 place
    bytecopy = bytecopy << 1 = b0101100 result
    
    shift result right 1 place
    reversed = reversed >> 1 = b00000000
    8 times no then up^ LOOP8
    8 times yes then done.
    

提交回复
热议问题