Exploring the mysteries of right shifting of negative arithmetic and right shifting of logic in javascript

Source: Internet
Author: User

The right shift of negative numbers in javascript and the right shift of logic are very confusing, especially the right shift of logic >>>, you will find that even if a small negative number, after the right shift, we can also get a huge number. Why?

It turns out that in the logical right shift, the symbol bit will move along with the whole to the right, which is equivalent to moving the unsigned number. The final result is a positive number because the symbol bit does not exist. First, the logical right shift must generate a 32-bit number, and then the negative sign bit is 1, which means that all the positions from 32nd to the sign bit are filled by 1, can this number be too small? For example, the logic shifts the right to 0 bits in the form of 1111 1111 1111 1111 1111 1111 1111 1111. Such a number is treated as a positive number! Therefore, the-1 logic is shifted to N places, and the final result is all 1!

The Left shift operation retains the sign bit of the number. For example, if you move-2 to the left to 5 digits, the result is-64 instead of 64. "Is the symbol still stored in the 32nd-bit format ?" Yes, but this is done in the ECMAScript background. Developers cannot directly access 32nd digits. Even if a negative value in the form of a binary string is output, it is displayed as a negative number (for example,-2 will display-10 .)

Bitwise operation

The shifted right operator is represented by two greater signs (<$ lt ;). It shifts all the digits in the 32-bit number to the right and retains the symbol (positive or negative) of the number ). The bitwise shift operator is the opposite of the bitwise shift operator. For example, to shift 64 to the right, change to 2:

Var iOld = 64; // equal to binary 1000000
Var iNew = iOld> 5; // equal to the binary value of 10 decimal 2. After the number is moved, the space is blank. This time, the blank space is located on the left side of the number, but behind the symbol bit. ECMAScript fills these spaces with the symbol bit values and creates a complete number, as shown in:

Unsigned right shift operation

The unsigned right shift operator is represented by three greater than signs (>>>). It shifts all digits of the unsigned 32-digit number to the right. For positive numbers, the result of the unsigned right shift operation is the same as that of the signed right shift operation.

In the example of a signed right shift operation, change 64 to 5 bits to 2:

The unsigned right shift operation fills all spaces with 0. For positive numbers, this is the same as the operation for the signed right shift operation, while the negative number is processed as a positive number.

Because the result of the unsigned right shift operation is a 32-bit positive number, the unsigned right shift operation of negative numbers always produces a very large number. For example, if you shift-64 to the right by 5 digits, 134217726 is returned. What if we get this result?

To achieve this, you need to convert the number into an equivalent form without a symbol (although the number itself is still signed). You can obtain this form using the following code:

Var iUnsigned64 =-64 >>> 0;

Then, use the toString () of the Number type to obtain its true bit representation. The base is 2: the code is as follows:

Alert (iUnsigned64.toString (2 ));

This will generate 11111111111111111111111111000000, that is, the binary complement representation of the signed integer-64, but it is equal to the unsigned integer 4294967232.

For this reason, be careful when using the unsigned right shift operator.

Now let's talk about the right shift of negative arithmetic >>:

We found-9> 2 =-3. Why is it-3?

First, the symbol bit remains unchanged and does not participate in the right shift. Then, if the first digit is 1 during the 9-right shift, the second digit is still 1 after the right shift! This is strange.

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.