11.11. 11.11 Merging Bit Strings

Another common bit string operation is producing a single bit string by merging, or interleaving, bits from two different sources. The following example code sequence creates a 32-bit string by merging alternate bits from two 16-bit strings:

// Merge two 16-bit strings into a single 32-bit string.
// AX - Source for even numbered bits.
// BX - Source for odd numbered bits.
// CL - Scratch register.
// EDX- Destination register.

               mov( 16, cl );
MergeLp:       shrd( 1, eax, edx );       // Shift a bit from EAX into EDX.
               shrd( 1, ebx, edx );       // Shift a bit from EBX into EDX.
               dec( cl );
               jne MergeLp;

This particular example merged two 16-bit values together, alternating their bits in the result value. For a faster implementation ...

Get Art of Assembly Language, 1st Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.