0

I am a newbie in javascript, one wired thing I meet is: the formula (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z); result is -529764813, but if i store each sum part into a temp variable, then add them, the result is different -529691705. Please have a look at the variables used in the formula and the result mx, new_mx are different. The code is executed and watched in chrome JS console. Who can help me ?

>z
6
>y
13106
>sum
-1640531527
>k
[1685024337, 1683575095, 1110798964, 6387041]
>p
0
>e
2
>mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
-529764813
>t1  = (z >>> 5 ^ y << 2)
52424
>t2=(y >>> 3 ^ z << 4) ^ (sum ^ y) 
-1640543091
>t3=(k[p & 3 ^ e] ^ z)
1110798962
>new_mx=t1+t2+t3
-529691705

2 Answers 2

2

Javascript only has float numbers, no integers. And bitwise operations in Javascript generally are not a good idea, see http://www.crockford.com/javascript/survey.html

For float arithmetic, see the classic What Every Computer Scientist Should Know About Floating-Point Arithmetic.

Sign up to request clarification or add additional context in comments.

1 Comment

+1. Didn't aware of this before. However, I still think the we can reliably use the bitwise operations, if the application absolutely needs it and the calculation involves only integer less than 53 bit precision (counting intermediary results).
2

https://developer.mozilla.org/en/JavaScript/Reference/Operators/Operator_Precedence

+ has higher precedence than ^, so the formula is grouped like this:

((z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4)) ^ ((sum ^ y) + (k[p & 3 ^ e] ^ z))

1 Comment

My godsh, I just realize that. JS is so different with other language operator priority. You helped me. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.