In JavaScript, >> and >>> are both right-shift operators, but they behave very differently with negative numbers.


1️⃣ 

>>

  

Arithmetic right shift

👉 Preserves the sign bit

a >> b
  • Shifts bits to the right

  • Keeps the leftmost (sign) bit

  • If number is negative, it fills with 1s

  • Works like division by 2 (rounded toward −∞)

Example

5 >> 1
// 5  = 00000000000000000000000000000101
// →  00000000000000000000000000000010
// = 2
-5 >> 1
// -5 = 11111111111111111111111111111011
// →   11111111111111111111111111111101
// = -3

✔ Keeps the number negative


2️⃣ 

>>>

  

Logical right shift

👉 Does NOT preserve the sign

a >>> b
  • Shifts bits to the right

  • Always fills with 0****s

  • Treats the number as unsigned 32-bit

  • Can turn negative numbers into large positives

Example

-5 >>> 1
// -5  = 11111111111111111111111111111011
// →     01111111111111111111111111111101
// = 2147483645

🚨 This is often surprising to people.


3️⃣ Key difference summary

OperatorNameSign preserved?Negative numbers
>>Arithmetic shift✅ YesStays negative
>>>Logical shift❌ NoBecomes large positive

4️⃣ Why 

>>

 is used in Counting Bits DP

dp[i] = dp[i >> 1] + (i & 1);
  • i is non-negative

  • i >> 1 safely means divide by 2

  • No risk of sign issues

❌ Using >>> here gives no benefit and can confuse readers.


5️⃣ Interview-ready one-liner ✅

>> preserves the sign bit (arithmetic shift), while >>> fills with zeros and treats the number as unsigned.


6️⃣ Rule of thumb

  • Use >> for math-like behavior (divide by 2)

  • Use >>> when working with raw bit patterns or unsigned values

If you want, I can also explain:

  • Why JS bitwise ops are 32-bit signed

  • Why 1 << 31 becomes negative

  • How this affects LeetCode solutions

Just say 👍