[英]Javascript bitwise to c#
I have some issue to convert a javascript code into c# the issue is with bitwise operator:我有一些问题将 javascript 代码转换为 c# 问题在于按位运算符:
Javascript function: return (s - (s | 0x0)) * 0x100000000 | Javascript function:返回 (s - (s | 0x0)) * 0x100000000 | 0x0; 0x0;
C# function; C# function; return (long)((s - ((long)s)) * 0x100000000);返回 (long)((s - ((long)s)) * 0x100000000);
If s = 1.7320508075688772 on Javascript report -1150833019 on c# report 3144134277如果 s = 1.7320508075688772 在 Javascript 上报告 -1150833019 在 c# 上报告 3144134277
other example can be Javascript: (1779033703 << 0x1e) = -1073741824 c# (1779033703 << 0x1e) = 1910222893216694272其他示例可以是 Javascript: (1779033703 << 0x1e) = -1073741824 c# (1779033703 << 0x1e) = 116942222893
What i need is translate Javascript function into c# with same number result.我需要的是翻译 Javascript function 成 c# 具有相同的数字结果。
Thanks for help.感谢帮助。
So, there are a few things going on here.所以,这里发生了一些事情。
You have a type mismatch in your JavaScript.您的 JavaScript 类型不匹配。 In Hex, 3144134277
is BB67AE85
, and -1150833019
is FFFFFFFFBB67AE85
.在十六进制中, 3144134277
是BB67AE85
, -1150833019
是FFFFFFFFBB67AE85
。 So, we can see that the JavaScript int32 is being implicitly converted to an unsigned int64.因此,我们可以看到 JavaScript int32 被隐式转换为 unsigned int64。
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
.您不能按 0 进行位移。位移除以2^n
,在这种情况下,n 为 0。返回的数字与2^0 = 1
相同。
(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. (long)((ulong)(...) 这是双重转换,被认为是错误的形式。您的数字文字将被转换为无符号长整数,然后再次转换为长整数。这只会浪费循环。
Your cast is a C style cast, in C# casting is more often done as object.ToInt()您的演员表是 C 风格演员表,在 C# 演员表中更常见的是 object.ToInt()
So, in review, you have a bug in your JavaScript.因此,在审查中,您的 JavaScript 中有一个错误。
You can't expect the same behavior on C# by default.默认情况下,您不能期望在 C# 上出现相同的行为。 Because:因为:
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.因此,在您的情况下,您可能会尝试将 64 位数字转换为 32 位数字并从那里得到错误的结果。 Which in C# it wouldn't be a good thing to have in my opinion.在我看来,在 C# 中,这不是一件好事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.