我正在查看Mozilla的代码,它为Array添加了一个过滤方法,它有一行代码让我很困惑.
var len = this.length >>> 0;
Run Code Online (Sandbox Code Playgroud)
我以前从未见过用于JavaScript的>>>.
它是什么,它做了什么?
这就是我要的:
>> var uint n = 0;
<- undefined (output by firefox dev console, I don't know what this means)
>> n;
<- 0;
>> n - 1;
<- 4294967295 (2^32-1)
Run Code Online (Sandbox Code Playgroud)
那么如何在javascript中声明一个变量,使其无符号?提前致谢。
这个问题与另一个问题类似;但是,我想了解为什么会这样。
下面的代码:
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x000000ff', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x0000ff00', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x00ff0000', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0xff000000', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x000000ff', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x0000ffff', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0x00ffffff', 16)).toString(16));
console.log((parseInt('0xdeadbeef', 16) & parseInt('0xffffffff', 16)).toString(16));
Run Code Online (Sandbox Code Playgroud)
返回:
ef
be00
ad0000
-22000000
ef
beef
adbeef
-21524111
Run Code Online (Sandbox Code Playgroud)
当我对 .string(16) 的期望是:
ef
be00
ad0000
de000000
ef
beef
adbeef
deadbeef
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?
预先感谢您的帮助。
感谢以下回复者和评论者,以及以下来源:
下面是一个解决方案,它通过提供实用函数来将 radix-16 32 位数字与有符号 32 位整数相互转换:
// Convert 'x' to …Run Code Online (Sandbox Code Playgroud)