简体   繁体   English

使用无符号32位整数的javascript中的位移位/类型转换?

[英]Bit shifting/type casting in javascript with unsigned 32 bit integers?

I'm trying to convert some complex C# bitshifting code into javascript (node), but I'm having issues with this kind of conversion as an example: 我正在尝试将一些复杂的C#移位代码转换为javascript(节点),但是例如,这种转换存在问题:

var d = false;
var k = 61;
var dd = 103;

uint r = 2924539136;
r |= unchecked((byte)(d ? (k + dd) : (k - dd)));

Console.WriteLine("result: " + r);  // 2924539350

Is there some way to replicate the (byte) casting in javascript to where it computes the value I'm looking for, I have this so far, but it just results in -42. 是否有某种方法可以将javascript中的(字节)类型转换复制到计算我想要的值的位置,到目前为止,我已经拥有了,但是它只会导致-42。

r |= (d ? (k + dd): (k - dd));
var d = false;
var k = 61;
var dd = 103;

r = 2924539136;

b = (d ? (k + dd) : (k - dd))
x = (r | (b & 0xFF)) >>> 0

returns 回报

x = 2924539350

The trick is to use &0xFF to mask all but lower 8 bits in b and >>>0 to convert a signed 32-bit number to unsigned . 诀窍是使用&0xFF屏蔽b除低8位之外的所有位,并使用>>>0 将有符号的32位数字转换为unsigned

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM