0

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?

0

1 Answer 1

3

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.

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

2 Comments

+1 or int bt = checked((16843134 ^ (16843134 - 1)) * j); to reveal the overflow
Woah, this is sweet. Always took this type of stuff for granit for so long until now when I really need to understand lower level things as Im converting some chess code to Javascript just as an exercise to learn more. Thanks a million.

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.