Mix of two bit sequences

Posted by Tomek Tarczynski on Stack Overflow See other posts from Stack Overflow or by Tomek Tarczynski
Published on 2010-04-02T12:27:38Z Indexed on 2010/04/02 12:33 UTC
Read the original article Hit count: 380

Filed under:

Is there any clever way to mix two bit sequences in such way that bits from first sequence will be on odd places, and bits from second sequence will be on even places.
Both sequences are no longer than 16b so output will fit into 32bit integer.

Example:

First sequence  : 1   0   0   1   0   0  
Second sequence :   1   1   1   0   1   1  
Output          : 1 1 0 1 0 1 1 0 0 1 0 1

I thought about making integer array of size 2^16 and then the output would be:

arr[first] << 1 | arr[second]

© Stack Overflow or respective owner

Related posts about bit-manipulation