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

Asked

Viewed 1,480 times

16

What is the function of mathematical operators <<, >> and >>> 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 it works the same >>

2 answers

19


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

<< pushes to the left and >> pushes right.

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

In practice, the operator x << n multiplies x by 2n (for example, x << 1 multiplies by 2) and the operator x >> n divided by 2n (for example, x >> 1 divide by 2, truncating the results).

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

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

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

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

Hence the representation of the number -2 is: 111111...111110. A number is positive if the most significant bit (the leftmost bit) is 0, and negative if the most significant bit is 1.

When applying -2 << 2 we’ve come to have 11111...111000. What is this number?

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

Soon -2 << 2 === -8. Again we see that the effect is to multiply by 2n.

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

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

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

We can thus say that the >> maintains the signal while >>> N always gives a positive number (provided that N > 0).

Documentation (in English)

  • +1 much more complete than my reply, thank you.

3

The operations of binary displacement function by moving in a binary representation of the number.

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

I used 4 bits only as teaching purposes for example.

In the case of signed numbers (which allow positive and negative, the displacement can be done to both sides), already for unsigned numbers >>> the operation can only be done 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 as positive as possible.

Example: -1 >>> 0 is 4294967295

For 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 by 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 the class Number and the method toString(2), example:

Number(-2 >>> 0).toString(2) // "11111111111111111111111111111110"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.