Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How does JavaScript calculate values and in wich order?

    Hello!

    Code:
    i=y=1;i<y;
    result:false (this makes sense to me)

    Im wondering about how JavaScript does the following calculations to get those results? Please explain it if you do. Id appreciate your time and help. Thank you!

    Code:
    i=y=1;y<<i;
    result:2

    Code:
    i=y=1;y>>i;
    result:0
    Last edited by Johan; 11-09-2011 at 04:04 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Have a look at

    https://developer.mozilla.org/en/Jav...wise_Operators


    Left shift a << b
    Shifts a in binary representation b (< 32) bits to the left, shifting in zeros from the right.

    Sign-propagating right shift a >> b
    Shifts a in binary representation b (< 32) bits to the right, discarding bits shifted off.

    What results were you expecting?


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    The << and >> are logical shift operators (left and respectively right); they are not arithmetical operators. That means your problem has nothing to do with the order of operations

    See also:
    http://www.c-point.com/javascript_tu...soprlshift.htm
    http://www.c-point.com/javascript_tu...soprrshift.htm
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    In fact I can think of few situations where bitwise operators are of any real use in Javascript.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I did not know what to expect. I just tried it and when i tried to figure it out my line of thought was something like this.

    Code:
    i=y=1;y<<i;
    The values of "i" and "y" is set to "1". "i" is greater than greater "y". The result should still be false if that was how JavaScript interpret "<<" as.) I dont know any practical use for this (my knowledge of programming is very limited) I was just being curious.

    Now i see that it did interpret "<<" as something else.

    It started with that JavaScript sets a value like this a=1, and it checks if av value is equal like this a==b then I just figured that greater than would be like this a>>b. Now i know that it is like this a>b. Then i was curious about what a>>b did.

    I have no concept of what you just wrote but it helped me understand my question. Thank you!
    Last edited by Johan; 11-09-2011 at 04:07 PM. Reason: spelling error


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •