JavaScript shift operators

Shift operation is carried out on a regular low binary shift. Shift operation can be designed many wonderful effects, widely used in the graphic image programming.

"<<" operator

"<<" operator performs left shift operation. In the shift operation during the sign bit remains the same. If the right vacated position, it is automatically filled with 0; the value exceeds 32 bits, is automatically discarded.
5 digital mobile two left, the return value is 20.

console.log(5 << 2); //返回值20

">>" operator

">>" operator performs a signed right shift operation. In contrast to the arithmetic left shift operation, it is all right overall significant bits of 32-bit numbers, the refill slot using the value of the sign bit. During movement beyond the values ​​will be discarded.

The value 1000 to the right 8, the return value is 3.

console.log(1000 >> 8); //返回值3

The values ​​to the right -1000 8, the return value is -4.

console.log(-1000 >> 8); //返回值 -4

">>>" operator

">>>" operator performs a right shift operation five symbols. It unsigned 32-bit integer all digits to the right integrally. For the unsigned right shift operation or a positive number, signed and unsigned right shift right operation results are the same.

The following two lines of expression of the return value are the same.

console.log(1000 >> 8);  //返回值3
console.log(1000 >> 8);  //返回值3

For negative numbers, the unsigned right shift using 0 to fill all the space, while a negative number would be treated as a positive, the results will be very large therefore, be careful when using the unsigned right shift operator, to avoid accidents .

console.log(-1000 >> 8);  //返回值 -4
console.log(-1000 >>> 8);  //返回值 16777212

Reprinted from: http://c.biancheng.net/view/5471.html

Guess you like

Origin www.cnblogs.com/hukuangjie/p/12651575.html