What is the most efficient way to de-interleave bits from a 32 bit int? For this particular case, I'm only concerned about the odd bits, although I'm sure it's simple to generalize any solution to both sets.
For example, I want to convert 0b01000101
into 0b1011
. What's the quickest way?
EDIT:
In this application, I can guarantee that the even bits are all zeros. Can I take advantage of that fact to improve speed or reduce space?
I'm not sure how quick it would be, but you could do something like
Which would pull all the odd bits off of a and put them on b.
In terms of speed, a lookup table 16 bits wide with 2^32 entries will be hard to beat! But if you don't have that much memory to spare, four lookups in a 256-entry table, plus a few shifts and ANDs to stitch them together, might be a better choice. Or perhaps the sweet spot is somewhere in between...it depends on the resources you have available, and how the cost of initializing the lookup table will be amortized over the number of lookups you need to perform.
Given that you know that every other bit is 0 in your application, you can do it like this:
The first step looks like this:
Then the second step works with two bits at a time, and so on.