JS shift operator __JS

Source: Internet
Author: User

1, left shift operation

The left shift operation is represented by a two less-than sign (<<). It moves all digits of the digital clock to the left by the specified number. For example, moving the number 2 to the left 5 bit does not result in 64 (equal to 1000000 in the binary).

var iOld = 2; Bits 10

var iNew = iOld << 5; Bits 1000000 equals 64.

Note that when you move the digits to the left, there are 5 more vacancies on the right side of the number. The left-shift operation fills these vacancies with 0, making the result a complete 32-digit number.

The left-shift operation retains the sign bit of the number. For example, if you move 2 to the left 5 digits, you get 64 instead of 64. Note that the symbol (the 32nd digit in the binary) is still stored in the 32nd digit. The left shift of code 2 above is as follows:

0000 0000 0000 0000 0000 0000 0000 0010 = 2

0000 0000 0000 0000 0000 0000 0100 0000 Left-shift 5-digit (blank place supplemented by 0) = 64

2, signed right shift operation

The signed right shift operator is represented by two greater-than (>>), which shifts all the digits in a 32-digit number to the right and retains the symbol (plus or minus) of that number. The signed right shift operation is exactly the opposite of the left shift operation. For example, moving 64 to the right 5-bit will change to 2:

var iOld = 64; Binary is: 1000000

var iNew = iOld >> 5; Binary is: 10, the result is 2

Also, moving a digit will result in an empty space. The vacancy is on the left of the number, but is after the sign bit (the 32nd digit in the binary). Empty spaces are populated with symbol bits: (that is, if the binary 32nd is 1, the vacancy is filled by 1, and vice versa.) )

0000 0000 0000 0000 0000 0000 0100 0000 = 64

0000 0000 0000 0000 0000 0000 0000 0010 = 2

3. Unsigned Right shift operation

The unsigned right shift is represented by three greater-than (>>>), which moves all the digits in the unsigned 32-digit integer to the right. For integers, the demerit of the unsigned Right shift operation is the same as that of a signed operation. With the example of a signed right shift operation, move 64 to the right 5 bit, and it will become 2. For negative numbers, the situation is different. The unsigned Right shift operation fills all vacancies with 0. For integers, this is exactly the same as the operation of a signed right shift operation, while negative numbers are treated as integers. Since the demerit of the unsigned Right shift operation is a 32-bit positive number, the unsigned right shift of the negative numbers is always a very large digit, such as moving 64 to the right 5 digits, with a result of 134217726.

To do this, you need to convert this number to an unsigned equivalence form (although the number is signed or has a minus sign), which can be obtained by using the following code:

var iUnsigned64 = -64 >>> 0;

Then, using the number type ToString () method to obtain its true bit representation, the base is 2:alert (Iunsigned64.tostring (2));

This will generate: 1111 1111 1111 1111 1111 1111 1100 0000, which is the binary complement of the signed integer-64, but it equals unsigned integer 4294967232. For this reason, be careful with the unsigned right shift operator.


Original address: http://blog.csdn.net/aerchi/article/details/23163119

This article prohibits collection.

Author Address: http://www.aerchi.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.