Search code examples
javajavascriptbit-manipulationbit-shift

what's the difference between bitwise shift with 2 arrows and 3 arrows?


I've seen >> and >>> before. What is the difference and when to use each?


Solution

  • Double Arrows ">>" and Triple Arrows ">>>" are defined on 32-bit integers, so performing these on a variable will "convert" them so-to-speak from non-numbers, to numbers. Additionally, javascript numbers are stored as double precision floats, so these operations will also cause you to lose any precision bits higher than 32 . ">>" maintains the sign bit (result is a signed integer), while ">>>" does not (result is an unsigned integer).

    http://msdn.microsoft.com/en-us/library/342xfs5s%28v=vs.94%29.aspx

    For a much better explanation: https://stackoverflow.com/a/1822769/780399