开发者

Why returns different value when use & operator in JavaScript and C#?

开发者_开发知识库I have worked the same process in JavaScript and C# with the & operator, but the result was different.

C# Code

Int64 x = (634586400000000000 & 4611686018427387903);
x= 634586400000000000;

JavaScript Code

var x = (634586400000000000 & 4611686018427387903);
x= 0;

Any ideas?


Bitwise operators in javascript convert the operands to signed 32-bit integers (from the native IEEE 754 floats numbers are stored in).


It looks to me like you're exceeding JavaScript's maximum integer value. The maximum supported value for JavaScript integers is spec'd at 2^53.

UPDATE:

My initial response here wasn't correct - the issue is not JavaScript's max integer value, it's the max value of each operand handled by the ampersand op:

var biggest = 4294967291; // maximum 32 bit unsigned integer
alert(biggest & 1); // alerts 1
alert((biggest + 1) & 1); // alerts 0

Happy coding!

B


The bitwise operators deal with a maximum of 32bits. I don't know how defined the behaviour is when asking it to deal with larger values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜