在回顾“Crossfilter”源时,我遇到了一个使用>>的函数。以下是功能:>>在javascript中做什么?
// Similar to bisectLeft, but returns an insertion point which comes after (to
// the right of) any existing entries of x in a.
//
// The returned insertion point i partitions the array into two halves so that
// all v <= x for v in a[lo:i] for the left side and all v > x for v in
// a[i:hi] for the right side.
function bisectRight(a, x, lo, hi) {
while (lo < hi) {
var mid = lo + hi >> 1;
if (x < f(a[mid])) hi = mid;
else lo = mid + 1;
}
return lo;
}
谷歌没有返回任何结果,我从来没有见过这种情况。先谢谢您的帮助。
这就像Math.floor(n/2)。在上面的例子中,注意'+'比位移有更高的优先级,所以'mid'将是'(lo + hi)>> 1'。 – Steve
如果你有将来的疑虑,你可以试着找出运行非常简单的代码(比如'a = 10; alert(a >> 1);')。 –