# The search for the right shift of negative arithmetic and logic in javascript

- 2020-03-26 21:29:22
- OfStack

The arithmetic and logical right shift of negative Numbers in javascript are very confusing, especially the logical right shift > > > And you can see that even a very small negative number, if you move it to the right, you're going to get a very large number. Why is that?

So in the logical shift to the right, the sign bit is going to move to the right with the whole thing, so it's going to be the same thing as the unsigned number, and you're going to end up with a positive number, because the sign bit isn't there anymore. Moves to the right first logic must be a 32-bit number, then the negative sign bit is 1, which means from the 32nd to all from 1 to fill the position of the sign bit, this number can not, for example - 1 form logic moves to the right 0 bit is 1111 1111 1111 1111 1111 1111 1111 1111, that number is treated as a positive number! So if you shift negative 1 to the right by N bits, you end up with all 1 factorial.

The left shift operation preserves the sign bit of the number. For example, if I move minus 2 5 bits to the left, I get minus 64 instead of 64. "Is the symbol still stored in bit 32?" Yes, but this happens in the ECMAScript background, and developers can't access the 32nd digit directly. Even if the output is negative in the form of a binary string, the display is in the form of a negative sign (for example, negative 2 will show negative 10).

The signed right shift operator is denoted by two greater than signs (< $lt;) . It moves all the digits in a 32-bit number to the right as a whole, while retaining the symbol (plus or minus) for that number. The signed right shift operator is the opposite of the left shift operator. For example, if you shift 64 to the right by 5 bits, it becomes 2:

Var iOld = 64; // is equal to binary 1000000

Var iNew = iOld > > 5; // is equal to binary 10, decimal 2, and again, if you move the digits, you're going to get a gap. This time, the empty space is to the left of the number, but behind the sign bit. ECMAScript populates these Spaces with symbolic bit values to create full Numbers, as shown in the following figure:

The unsigned right shift operator consists of three greater than signs (> > >) Means that it moves all the digits of the unsigned 32 digits to the right as a whole. For positive Numbers, the result of an unsigned right shift operation is the same as a signed right shift operation.

Using the example from the signed right shift operation, move 64 to the right by 5 bits to become 2:

The unsigned right shift operation fills all Spaces with 0. For positive Numbers, this is the same operation as a signed shift to the right, while negative Numbers are treated as positive Numbers.

Since the result of an unsigned right shift is a 32-bit positive number, the result of an unsigned right shift of a negative number is always a very large number. For example, if you shift -64 to the right by 5 bits, you get 134217726. What if I get this result?

To do this, you need to convert the number to its unsigned equivalent (although the number itself is still signed), which you can obtain by following the code:

Var iUnsigned64 = -64 > > > 0;

Then, toString() of type Number is used to get its true bit representation, with the base 2: code as follows:

Alert (iUnsigned64. ToString (2));

This will generate 11111111111111111111111111000000 that there are two's complement signed integer - 64, said it is unsigned integer 4294967232.

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

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

We find that -9> > 2 is minus 3. Why is it minus 3?

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

So in the logical shift to the right, the sign bit is going to move to the right with the whole thing, so it's going to be the same thing as the unsigned number, and you're going to end up with a positive number, because the sign bit isn't there anymore. Moves to the right first logic must be a 32-bit number, then the negative sign bit is 1, which means from the 32nd to all from 1 to fill the position of the sign bit, this number can not, for example - 1 form logic moves to the right 0 bit is 1111 1111 1111 1111 1111 1111 1111 1111, that number is treated as a positive number! So if you shift negative 1 to the right by N bits, you end up with all 1 factorial.

The left shift operation preserves the sign bit of the number. For example, if I move minus 2 5 bits to the left, I get minus 64 instead of 64. "Is the symbol still stored in bit 32?" Yes, but this happens in the ECMAScript background, and developers can't access the 32nd digit directly. Even if the output is negative in the form of a binary string, the display is in the form of a negative sign (for example, negative 2 will show negative 10).

**Signed right shift operation**The signed right shift operator is denoted by two greater than signs (< $lt;) . It moves all the digits in a 32-bit number to the right as a whole, while retaining the symbol (plus or minus) for that number. The signed right shift operator is the opposite of the left shift operator. For example, if you shift 64 to the right by 5 bits, it becomes 2:

Var iOld = 64; // is equal to binary 1000000

Var iNew = iOld > > 5; // is equal to binary 10, decimal 2, and again, if you move the digits, you're going to get a gap. This time, the empty space is to the left of the number, but behind the sign bit. ECMAScript populates these Spaces with symbolic bit values to create full Numbers, as shown in the following figure:

**Unsigned right shift operation**The unsigned right shift operator consists of three greater than signs (> > >) Means that it moves all the digits of the unsigned 32 digits to the right as a whole. For positive Numbers, the result of an unsigned right shift operation is the same as a signed right shift operation.

Using the example from the signed right shift operation, move 64 to the right by 5 bits to become 2:

The unsigned right shift operation fills all Spaces with 0. For positive Numbers, this is the same operation as a signed shift to the right, while negative Numbers are treated as positive Numbers.

Since the result of an unsigned right shift is a 32-bit positive number, the result of an unsigned right shift of a negative number is always a very large number. For example, if you shift -64 to the right by 5 bits, you get 134217726. What if I get this result?

To do this, you need to convert the number to its unsigned equivalent (although the number itself is still signed), which you can obtain by following the code:

Var iUnsigned64 = -64 > > > 0;

Then, toString() of type Number is used to get its true bit representation, with the base 2: code as follows:

Alert (iUnsigned64. ToString (2));

This will generate 11111111111111111111111111000000 that there are two's complement signed integer - 64, said it is unsigned integer 4294967232.

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

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

We find that -9> > 2 is minus 3. Why is it minus 3?

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