SHR
By Vesper Vei
1 minute read
Table of Contents
SHR(shr)
Basic purpose
SHR (logical right shift) shifts the operand right by n bits, filling the left side with 0s.
Mathematically: dest = floor(dest / 2^n)。
Instruction execution process
- Shift right the specified number of times
- Fill the left side with 0s
- Update CF, ZF, SF, OF
- The last shifted-out bit → CF
Instruction format
shr r/m32, imm8shr r/m64, imm8shr r/m32, clshr r/m64, clBehavioral characteristics
- Does not preserve the sign → not suitable for signed division
- Commonly used for unsigned integers
- Clearly different from SAR (SAR preserves the sign in the highest bit)
Common uses
- Fast division by 2^n (unsigned)
- Bitmask cleanup
- Right-shifting fields in protocol parsing
- Patch points in certain encryption/encoding algorithms