Left and right shifts are often used for fast multiplication/division (by 2) and bit manipulation. Since they can be done so quickly in hardware, compliers will perform strength reduction to optimize code to take advantage of shifts.
Note that there are two types of right shifts:
- Logical right shift puts 0 in the MSB.
- Arithmetic right shift puts whatever the original MSB is. This preserves the sign of the integer value.
A simple shifter that moves by a constant
Barrel Shifter
The barrel shifter provides a faster way to shift by