Codacoda
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

MetricBestAverageWorst
TimeO(1)O(1)O(1)
SpaceO(1)

Visualization

Start50000010176543210
Speed:1x
Starting value: 5 (00000101)Step 1 / 6

Implementation

Output

Click "Run Code" to see output...