What is the function of the operators, and in Javascript

13

What is the function of the mathematical operators << , >> and >>> in Javascript?

Examples:

  • 0 or 1 >> 1 is 0
  • 2 or 3 >> 1 is 1
  • 4 or 5 >> 1 is 2

Same goes for negative numbers.

Already:

  • -1 >>> 1 is 2147483647

And for positives works the same as >>

    
asked by anonymous 01.03.2014 / 21:18

2 answers

17

These are the bit shifting operators. That is, operators that change the value of the bit representation by "pushing" them to one side.

% of pushes to the left and << pushes to the right.

The number 5, for example, has >> representation. 000...00000101 moves the bits two positions (zeros filling the new places), so we have 5 << 2 , that is, 20.
000...00010100 moves the bits two positions to the other side, so we have 5 >> 2 , that is, 1.

In practice, the 000...00000001 operator multiplies x << n by 2 n (for example, x multiplies by 2) and the x << 1 operator divides by 2 n (for example, x >> n splits by 2, truncating the results).

All these operators convert the values of the operands to 32-bit integers before applying operations. If the right operand is greater than 32, only the 5 significant bits of that operand are used (therefore, x >> 1 is equal to x >> 2 )

To understand how these operators behave with negative numbers, it is important to realize how negative numbers are represented in memory. The format used by these operators assumes a complement of two template.

In this model, reversing the sign of a number is equivalent to inverting all bits and then adding 1. Example:

Número 2:                 00000...000010
Invertendo todos os bits: 11111...111101
Somando 1:                11111...111110

Thus, the representation of the number x >> 66 is: -2 . A number is positive if the most significant bit (the leftmost) is 0, and negative if the most significant bit is 1.

When you apply 111111...111110 we have -2 << 2 . What number is this?

Número X:                 11111...111000
Invertendo todos os bits: 00000...000111
Somando 1:                00000...001000 (que significa 8)

Logo 11111...111000 . Again we see that the effect is to multiply by 2 n .

The difference between -2 << 2 === -8 and >> is that >>> fills the bits entered on the left with the value of the most significant bit of the left operand, while >> fills the bits entered with 0.

>>> gives -4 >> 1 , but -2 gives:

Número 4:  0000...000100
Número -4: 1111...111100
Resultado: 0111...111110 (ou seja, 2^31 - 2)

So we can say that -4 >>> 1 holds the signal, while >> always gives a positive number (since N> 0).

Documentation

    
01.03.2014 / 21:45
1

Binary shift operations work by shifting in a binary representation of the number.

For example, the value 2 0010 or 3 0011 when moved to the right forms the value 1 0001 because the last number ends up being removed and the system will insert a 0 to compensate in the beginning.

I used 4 bits for example for example purposes.

In case of signed numbers (that allow positive and negative, the displacement can be done for both sides), for unsigned numbers >>> the operation can only be done to the right.

Because it does not contain the digit that identifies whether the value is positive or negative, this value will always be positive and since it is less than zero, it becomes the largest possible positive number.

Example: -1 >>> 0 is 4294967295

Because 0 is 0000 0000 0000 0000 0000 0000 0000 0000 and -1 in case it would change the signature, which causes a complete inversion, pushing all zeros and replacing them with 1 which results in 1111 1111 1111 1111 1111 1111 1111 1111 .

To test the binary values obtained a simple solution is the use of class Number and method toString(2) , example:

Number(-2 >>> 0).toString(2) // "11111111111111111111111111111110"
    
01.03.2014 / 21:35