I have a 16 bit value with its bits "interlaced".
I want to get an array of 8 items (values 0 to 3) that stores the bits in this order:
- item 0: bits 7 and 15
- item 1: bits 6 and 14
- item 2: bits 5 and 13
- ...
- item 7: bits 0 and 8
This is a trivial solution:
function uninterlace(n) {
return [((n>>7)&1)|((n>>14)&2), // bits 7 and 15
((n>>6)&1)|((n>>13)&2), // bits 6 and 14
((n>>5)&1)|((n>>12)&2), // bits 5 and 13
((n>>4)&1)|((n>>11)&2), // bits 4 and 12
((n>>3)&1)|((n>>10)&2), // bits 3 and 11
((n>>2)&1)|((n>> 9)&2), // bits 2 and 10
((n>>1)&1)|((n>> 8)&2), // bits 1 and 9
((n>>0)&1)|((n>> 7)&2)];// bits 0 and 8
}
Does anyone knows a better (faster) way of doing this?
Edit:
Notes:
- Build a precalculated table is not an option.
- Cannot use assembler or CPU-specific optimizations