在回顧「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);')。 –