0

I have some issue to convert a javascript code into c# the issue is with bitwise operator:

Javascript function: return (s - (s | 0x0)) * 0x100000000 | 0x0;

C# function; return (long)((s - ((long)s)) * 0x100000000);

If s = 1.7320508075688772 on Javascript report -1150833019 on c# report 3144134277

other example can be Javascript: (1779033703 << 0x1e) = -1073741824 c# (1779033703 << 0x1e) = 1910222893216694272

What i need is translate Javascript function into c# with same number result.

Thanks for help.

4
  • I don’t know C# syntax that well, but (int)3144134277U or (int)3144134277UL, maybe? >> produces a signed 32-bit integer in JavaScript when used on a number. Commented Sep 23, 2019 at 20:24
  • you cant use int cause is over range (32-bit signed integer type -2,147,483,648 to 2,147,483,647) Commented Sep 23, 2019 at 20:34
  • Do you know what the purpose of this operation is? It looks like you're trying to reinterpret an int as a uint and sign-extend it to a long. Whatever it is, though, there's probably a more natural, idiomatic way of doing in C# whatever it's intended to accomplish but we need to examine its purpose first. Commented Sep 23, 2019 at 20:43
  • thanks for your reply i edited description Commented Sep 23, 2019 at 20:51

2 Answers 2

3

So, there are a few things going on here.

  1. You have a type mismatch in your JavaScript. In Hex, 3144134277 is BB67AE85, and -1150833019 is FFFFFFFFBB67AE85. So, we can see that the JavaScript int32 is being implicitly converted to an unsigned int64.

  2. You can't bitshift by 0. Bitshifting is dividing by 2^n, where n is, in this case, 0. That returns the same number, as 2^0 = 1.

  3. (long)((ulong)(…) That's a double cast, and is considered bad form. Your number literal will be cast to an unsigned long, then cast again to a long. This just wastes cycles.

  4. Your cast is a C style cast, in C# casting is more often done as object.ToInt()

So, in review, you have a bug in your JavaScript.

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

Comments

1

You can't expect the same behavior on C# by default. Because:

  • In JavaScript, a number is stored as a 64-bit floating point number but the bit-wise operation is performed on a 32-bit binary number
  • So to perform a bit-operation JavaScript converts the number into a 32-bit binary number, perform the operation and convert back the result to a 64-bit number.

So in your case you might be trying to cast a 64-bit number to 32-bit one and get a faulty result from there. Which in C# it wouldn't be a good thing to have in my opinion.

1 Comment

This underscores the need to understand the function's purpose and try to match that instead of doing a direct translation.

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.