<<< SHL Example | Index | Extended-Precision Integers and Shifting Multiple Doublewords >>> |
One of the best uses of SHL is performing high-speed multiplication by powers of 2.
Shifting any operand left by n bits multiplies the operand by 2n.
For example, shifting a VALUE one bit to the left yields the product
VALUE * 2
Shifting a VALUE two bits to the left yields the product of
VALUE * 4
and so on.
mov eax, 10 shl eax, 2 ; 10 * 22 == 10 * 4 == 40
Shifts are more efficient to execute than the corresponding multiplication or division instructions. Some multiplication instructions could take more than 10 clock cycles. By contrast, SHL executes in just one clock cycle, and requires fewer bytes to encode. Whenever possible, use the shift instructions to perform multiplication and division by a power of two.
<<< SHL Example | Index | Extended-Precision Integers and Shifting Multiple Doublewords >>> |