algorithm behind the generation of the reverse bits lookup table(8 bit)

后端 未结 3 454
余生分开走
余生分开走 2021-02-04 13:17

I found the lookup table here. The table is generated as a reverse bits table of 8 bits.

I can not figure out why it works. Please explain the theory behind it. Thanks<

3条回答
  •  情书的邮戳
    2021-02-04 13:56

    If you are working with Python and happen to end up here, this is how the lookup table would look like. Nevertheless, Bernd Elkemann's explanation still stands.

    # Generating the REVERSE_BIT_LUT while pre-processing
    # LUT is shorthand for lookuptable
    def R2(n, REVERSE_BIT_LUT):
        REVERSE_BIT_LUT.extend([n, n + 2 * 64, n + 1 * 64, n + 3 * 64])
    
    
    def R4(n, REVERSE_BIT_LUT):
        return (
            R2(n, REVERSE_BIT_LUT),
            R2(n + 2 * 16, REVERSE_BIT_LUT),
            R2(n + 1 * 16, REVERSE_BIT_LUT),
            R2(n + 3 * 16, REVERSE_BIT_LUT),
        )
    
    
    def R6(n, REVERSE_BIT_LUT):
        return (
            R4(n, REVERSE_BIT_LUT),
            R4(n + 2 * 4, REVERSE_BIT_LUT),
            R4(n + 1 * 4, REVERSE_BIT_LUT),
            R4(n + 3 * 4, REVERSE_BIT_LUT),
        )
    
    
    def LOOK_UP(REVERSE_BIT_LUT):
        return (
            R6(0, REVERSE_BIT_LUT),
            R6(2, REVERSE_BIT_LUT),
            R6(1, REVERSE_BIT_LUT),
            R6(3, REVERSE_BIT_LUT),
        )
    
    
    # LOOK_UP is the function to generate the REVERSE_BIT_LUT
    REVERSE_BIT_LUT = list()
    LOOK_UP(REVERSE_BIT_LUT)
    

    I am working on providing code snippets of Sean's bit hacks in Python here.

提交回复
热议问题