什么是x>>> 0呢? [英] What does x >>> 0 do?

查看:1432
本文介绍了什么是x>>> 0呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

零填充位移0有什么用呢? (a>>> 0)

我一直在尝试我的一个项目中的一些函数式编程概念,我正在阅读 Array.prototype.map ,这是ES5中的新功能,如下所示:

I've been trying out some functional programming concepts in a project of mine and I was reading about Array.prototype.map, which is new in ES5 and looks like this:

Array.prototype.map = function(fun) {
    "use strict";
    if (this === void 0 || this === null) {
        throw new TypeError();
    }
    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun !== "function") {
        throw new TypeError();
    }
    var res = new Array(len);
    var thisp = arguments[1];
    for (var i = 0; i < len; i++) {
        if (i in t) {
            res[i] = fun.call(thisp, t[i], i, t);
        }
    }
    return res;
};

我想知道为什么它正在做 t.length>> ;> 0 。因为它似乎没有做任何事情。 x>>> 0 // - > X ! (只要x是一个数字,显然)

What I'm wondering is why it's doing t.length >>> 0. Because it doesn't seem to do anything. x >>> 0 //-> x! (as long as x is a number, obviously)

另外,请注意我不知道按位运算符是如何工作的。

Also, note that I don't know how bitwise operators work.

推荐答案

x>>> 0 执行0位的逻辑(无符号)右移,相当于无操作。但是,在右移之前,它必须将 x 转换为无符号的32位整数。因此, x>>>的整体效果0 x 转换为32位无符号整数。

x >>> 0 performs a logical (unsigned) right-shift of 0 bits, which is equivalent to a no-op. However, before the right shift, it must convert the x to an unsigned 32-bit integer. Therefore, the overall effect of x >>> 0 is convert x into a 32-bit unsigned integer.

这可确保 len 是非负数。

js> 9 >>> 0
9
js> "9" >>> 0
9
js> "95hi" >>> 0
0
js> 3.6 >>> 0
3
js> true >>> 0
1
js> (-4) >>> 0
4294967292

这篇关于什么是x&gt;&gt;&gt; 0呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆