Back to Academy
bitwise operations
Left Shift & Right Shift
Bit shifting moves all bits in a number left or right by a specified number of positions. Left shift (<<) multiplies by powers of 2, while right shift (>>) divides by powers of 2. JavaScript also provides the unsigned right shift (>>>) which fills with zeros regardless of sign.
Use Cases
- •Fast multiplication and division by powers of 2
- •Packing and unpacking data into fixed-width fields
- •Color channel extraction from RGB/RGBA values
- •Computing array indices in hash tables
Complexity Analysis
| Metric | Best | Average | Worst |
|---|---|---|---|
| Time | O(1) | O(1) | O(1) |
| Space | O(1) | ||
Visualization
Implementation
Output
Click "Run Code" to see output...