Course contentsShow
← JavaScript
Lesson 260 of 2,7237. Arithmetic and OperatorsFree lesson

Sign-Propagating Right Shift (>>)

Learn how >> shifts bits right while preserving the sign bit for negative numbers.

Sign-Propagating Right Shift (>>)

You'll learn how the >> operator shifts bits to the right while keeping negative numbers negative.

What It Does

The sign-propagating right shift operator (>>) moves all bits in a number to the right by a specified number of positions. The key feature: it preserves the sign bit (the leftmost bit that indicates positive or negative). When shifting right, it fills the leftmost positions with copies of the sign bit instead of zeros.

How It Works

Think of bits sliding right off a cliff, and the sign bit duplicating itself to fill the gaps on the left:

8 >> 1   // Result: 4
// Binary: 00001000 becomes 00000100

-8 >> 1  // Result: -4
// Binary: 11111000 becomes 11111100 (sign bit preserved)

For positive numbers, zeros fill from the left (like left shift, but reversed). For negative numbers, ones fill from the left, keeping the number negative.

Practical Usage

16 >> 2   // Result: 4 (divide by 4)
-16 >> 2  // Result: -4 (divide by 4, sign preserved)

100 >> 3  // Result: 12
-100 >> 3 // Result: -13 (rounds toward negative infinity)

The Math Shortcut

Each right shift by 1 position effectively divides by 2 (rounding down toward negative infinity):

32 >> 1   // 16
32 >> 2   // 8
32 >> 3   // 4

Key Takeaway: The >> operator shifts bits right while copying the sign bit leftward, ensuring negative numbers stay negative—essentially dividing by powers of 2 while preserving the number's sign.