How can I shuffle bits efficiently?

前端 未结 7 1178
闹比i
闹比i 2020-12-29 06:15

I need to shuffle a 16 bit unsigned integer in a way that the even indexes land in the lower byte, and the odd indexes land in the upper byte.

input:
fedcba9         


        
7条回答
  •  有刺的猬
    2020-12-29 06:51

    You could use a 256-byte table for each byte of your 16-bit number, crafted so that your even/odd condition is satisfied. Hand-code the table entries (or use the algorithm you already have) to create the tables, and then the shuffling will be done at compile time. That would essentially be a translation table concept.

提交回复
热议问题