# [ACCEPTED]-Strange javascript operator: expr >>> 0-operators

`>>>`

is the Zero-fill right shift operator. The `>>> 0`

is an abuse of the 21 operator to convert any numeric expression 20 to an "integer" or non-numeric 19 expression to zero. Here is what it does:

This 18 operator shifts the first operand the specified 17 number of bits to the right. Excess bits 16 shifted off to the right are discarded. Zero bits 15 are shifted in from the left. The sign bit 14 becomes 0, so the result is always positive.

Here is an explanation of 13 the convert-to-integer behavior which applies 12 to all bitwise operations:

Bitwise operators 11 treat their operands as a sequence of 32 10 bits (zeros and ones), rather than as 9 decimal, hexadecimal, or octal numbers. [...] Bitwise 8 operators perform their operations on such 7 binary representations, but they return 6 standard JavaScript numerical values.

Together, these 5 statements assert that `expr >>> 0`

will always return 4 a positive number as follows:

`expr`

is cast to a 32-bit integer for bitwise operation`>>> 0`

has no effect (no bits are shifted)- The result is converted to a
`Number`

Here are a 3 few expressions and their outcome:

```
1 >>> 0 // 1 -- Number cast to 32-bit integer then back to Number
"1" >>> 0 // 1 -- String cast to 32-bit integer then back to Number
undefined >>> 0 // 0 -- failed cast yields zero
```

Other 2 interesting cases:

```
1.1 >>> 0 // 1 -- decimal portion gets it
-1 >>> 0 // 4294967295 -- -1 = 0xFFFFFFFF
// Number(0xFFFFFFFF) = 4294967295
"A" >>> 0 // 0 -- cast failed
"1e2" >>> 0 // 100 -- 1x10^2 is 100
"1e10" >>> 0 // 1410065408 -- 1x10^10 is 10000000000
// 10000000000 is 0x00000002540BE400
// 32 bits of that number is 0x540BE400
// Number(0x540BE400) is 1410065408
```

Note: you will notice 1 that none of them return `NaN`

.

Source: LINK

This is the zero-fill right shift operator 11 which shifts the binary representation 10 of the first operand to the right by the 9 number of places specified by the second 8 operand. Bits shifted off to the right 7 are discarded and zeroes are added on 6 to the left. With a positive number you 5 would get the same result as with the sign-propagating 4 right shift operator, but negative numbers 3 lose their sign becoming positive as in 2 the next example, which (assuming 'a' to 1 be -13) would return 1073741820:

Code:

`result = a >>> b;`

The `>>>`

(right-shift) binary operator is simply 13 shifting the right-most bits of a number 12 a specified number of times, and padding 11 with zeroes to the left.

Note: In the following 10 examples, the number in braces after a number 9 signals what base it's in. 2 is for binary, 10 8 for decimal.

For example, `4 >>> 1`

would do:

```
4(10) = 100(2)
4(10) >>> 1(10) = 010(2) = 2(10)
shift once to the right
```

Other 7 examples:

```
4(10) >>> 2(10) = 100(2) >>> 2(10) = 001(2) = 1(10)
10(10) >>> 4(10) = 1010(2) >>> 4(10) = 0000(2) = 0(10)
15(10) >>> 1(10) = 1111(2) >>> 1(10) = 0111(2) = 7
```

The way I remember it is to move 6 the necessary amount of bits to the right, and 5 then write the number. Like, in the last 4 example, I simply moved everything to the 3 right once, so the result is 0111.

Shifting 2 0 times does...nothing. No idea why it's 1 there.

Behold the zero-fill right-shift operator.

https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators

0

More Related questions

We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.