1

There is code on javascript

const a = 2654435769;        
const b = 7562089;        
console.log(a ^ b); // answer -1639703856

When you try to rewrite this code on C #, I ran into a problem.

long a = 2654435769;
long b = 7562089;
var result = a ^ b; // result = 2655263440;

Why do you get answers in different languages? And how to get the same answer on C # on JavaScript

2
  • Probably by using an int instead of a long (don't know C#). JS uses 32bit signed integer bitwise operations. Commented Aug 28, 2021 at 21:30
  • 1
    It's a bitwise XOR operator, not a logical XOR operator. Javascript converts numbers into signed 32-bit integers to perform bitwise operators. Commented Aug 28, 2021 at 21:33

1 Answer 1

3

The ECMAScript Specification describes that for all bitwise operations on numbers, they will be converted to 32 bit integers before applying the operator. To obtain the same result in C# you can cast your operands to int:

long a = 2654435769;
long b = 7562089;
var result = (int)a ^ (int)b; // result = -1639703856;

Relevant bits from the spec in case the link goes stale in the future:

Number::bitwiseXOR

NumberBitwiseOp

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.