kodejuice Posted February 15, 2015 Share Posted February 15, 2015 Please can one briefly explain the use of this sign in javascript, and what is results to.I.e var d = 5;return d >> 1; //=> 10why is it '10'? Link to comment Share on other sites More sharing options...
Ingolme Posted February 15, 2015 Share Posted February 15, 2015 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. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now