Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does >>>= mean? [duplicate]

While I was checking the lodash code, I came up with a question I was curious about.

// slice.js
function slice(array, start, end) {
  let length = array == null ? 0 : array.length
  if (!length) {
    return []
  }
  start = start == null ? 0 : start
  end = end === undefined ? length : end

  if (start < 0) {
    start = -start > length ? 0 : (length + start)
  }
  end = end > length ? length : end
  if (end < 0) {
    end += length
  }
  length = start > end ? 0 : ((end - start) >>> 0)
  start >>>= 0

  let index = -1
  const result = new Array(length)
  while (++index < length) {
    result[index] = array[index + start]
  }
  return result
}

export default slice
  1. Specifically, I am curious about why this code exists in this part.
((end - start) >>> 0)

As far as I know, the bit operator shifts the position of a binary number, but I am curious why it shifts 0 times.

  1. >>>=
  • And >>>= This operator is the first time I saw it. Does anyone know what it means?
like image 477
kim Joy Avatar asked Jan 08 '21 07:01

kim Joy


1 Answers

>>> is called unsigned right shift (or zero fill right shift). You may have known of left/right shift. Basically it shifts the specified number of bits to the right. You can find it here

So >>>= is basically the right shift assignment.

a >>>= b

is equivalent to:

a = a >>> b
like image 71
Minh Nguyen Avatar answered Sep 24 '22 17:09

Minh Nguyen