Jump to content

The use of the "<<" and the ">>" operator in JavaScript.


kodejuice

Recommended Posts

If you represent the number in binary you'll get a series of 0s and 1s. The >> and << operators move the 0s and 1s to the right and left, respectively.

 

5 in binary is 00000101.

If you shift them to the right by 1 the result is 00000010 (in binary) which is 2 in our system. The last 1 disappeared because it's outside of the bits that belong to the number.

If you shift 5 to the left by 1 it would be 00001010 in binary (empty spaces get filled in with zeroes), which is 10 our system.

 

Shifting to the left or right is equivalent to multiplying and dividing by two, except the computer can do the operation much faster.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...