0

I'm trying to figure out the fastest way to copy bits according to a given mask, and put them together, for example:

           |  | |
x    = 0x01110001
           |  | |
mask = 0x00100101
           |  | |

result=0x101----- (- doesn't matter)

basically I want to copy all the bits for which there is a 1 in the mask and shift them together.

I can only think of a slow and tedious way to copy each bit and shift it.

So the question is there a faster strait forward way to do it for all the bits simultaneously?

MoonBun
  • 4,322
  • 3
  • 37
  • 69
  • Seems to me your example is wrong - what happened to the leftmost bit in the mask and x? – Elemental Aug 03 '15 at 08:07
  • See chapter 7 in [Hacker's Delight](http://www.amazon.com/Hackers-Delight-Edition-Henry-Warren/dp/0321842685) section 7-4, "Compress, or Generalized Extract" - it describes an efficient method of implementing exactly this operation. – Paul R Aug 03 '15 at 08:08

0 Answers0