user1039705
user1039705

Reputation: 1

Javascript Bitwise Operators

I'm trying to figure out some differences between C# and Javascript. Ok, take this code in Javascript:

var j = 0x783a9b23;
var bt = ((16843134 ^ (16843134 - 1)) * j);

After executing this, "bt" will be 6051320169.

Now after doing this in C#:

int j = 0x783a9b23;
int bt = ((16843134 ^ (16843134 - 1)) * j);

"bt" will be 1756352873. Certainly not the same. Any ideas why Javascript is not seeing how C# sees it?

Upvotes: 0

Views: 260

Answers (1)

Dogbert
Dogbert

Reputation: 222428

You can do this to make it work like in C#

var j = 0x783a9b23;
var bt = ((16843134 ^ (16843134 - 1)) * j);
bt = bt % 2147483647

This is because in c# your integer overflows the limit of 2,147,483,647.

Upvotes: 3

Related Questions