The arithmetic right shift and logical right shift of negative numbers in JavaScript are very confusing, especially the logical right shift >>>, you will find that even a very small negative number, after the right shift, will also get a huge amount, this is why?

Originally in the logical right shift symbol bit will move to the right along with the whole, so that is the equivalent of unsigned number of move, and finally get a positive, because the symbol bit does not exist. First, the logical right shift produces a number of 32 digits, then the negative sign bit is 1, which means that the position from 32nd to the sign bit is filled by 1, so does that count to be small? For example-1, the logical right shift 0-bit representation is 1111 1111 1111 1111, 1111 1111 1111, Such numbers are treated as positive numbers! So the 1 logic right shift n bit, 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 5 digits, you get 64 instead of 64. is the symbol still stored in the 32nd bit? "Yes, but this is done in the background of ECMAScript, and developers cannot access the 32nd digit directly." Even a negative number in the form of an output binary string is shown as a minus sign (for example,-2 will show-10). ）

A signed right shift operation

The signed right shift operator is represented by a two greater-than number (< $lt;). It shifts all the digits in a 32-digit number to the right, while preserving the symbol (plus or minus) for that number. The signed right shift operator 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; equals binary 1000000
var iNew = iOld >> 5; is equal to binary 100 in 2. Also, moving a digit creates an empty space. This time, the vacancy is located to the left of the number, but after the symbol bit. ECMAScript fills these vacancies with the value of the symbol bit, creating the full number, as shown in the following figure:

unsigned Right shift operation

The unsigned Right shift operator is represented by three greater-than sign (>>>), which moves all digits of the unsigned 32-digit integer right. For positive numbers, the result of the unsigned Right shift operation is the same as the signed right shift operation.

With the example in a signed right shift operation, move 64 to the right 5 bit, and it will become 2:

The unsigned Right shift operation fills all vacancies with 0. For positive numbers, this is the same as the operation of a signed right shift operation, while negative numbers are treated as positive numbers.

Because the result of an unsigned right shift operation is a 32-bit positive number, the unsigned right shift of negative numbers is always a very large digit. For example, if you move 64 to the right 5 digits, you get 134217726. What if you get the results?

To do this, you need to convert this number to an unsigned equivalent (although the number itself is still signed), you can get this form by using the following code:

var iUnsigned64 = -64 >>> 0;

Then, using the number type toString () to get its real bit representation, the base is 2: The code is as follows:

Alert (iunsigned64.tostring (2));

This generates 11111111111111111111111111000000, the binary complement representation of the signed integer-64, but it equals unsigned integer 4294967232.

For this reason, use the unsigned right shift operator with care.

Now let's say the arithmetic of negative numbers right shift >>:

We found -9>>2=-3, why-3?

First sign bit is invariant, do not participate in the right shift, and then in the process of 9 to the right, the lowest bit is 1, then the lowest bit after the right shift is still 1! This is very strange.

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.