title: How to replicate PEXT (parallel bit extract) with basic bitwise ops?
body: <p>I'm porting some code from Rust to Swift and I noticed they use pext method to do some bit manipulation. Since it's an Intel instruction I can't get that into my Swift code so I was wondering what set of bitwise instructions (that I can use in Swift) would be able to get the same result as a <code>PEXT</code> operation.</p>
  
  <p>For more context, here is a description of <a href="https://en.wikipedia.org/wiki/Bit_Manipulation_Instruction_Sets#Parallel_bit_deposit_and_extract" rel="nofollow noreferrer"><code>PEXT</code></a></p>
  
  <p>Basically it uses a mask that maps high order bits to contiguous lower order bits and clears the higher order bits in the final output. Example: </p>
  
  <pre><code>source = 1011 1110 1001 0011
  mask =   0110 0011 1000 0101 
  
  s = pext(source, mask)
  
  s -&gt; 0000 0000 0011 0101 
  </code></pre>
  
  <p>I know replicating an advanced instruction is a performance hit but I'm really just trying to get the code to work. </p>