What is the function of the ~ (til) Javascript operator?

Asked

Viewed 1,727 times

38

I tried to search for Google and SOEN, but I did not find any reference to the operator ~, I discovered that it exists because I was reading a book about Javascript and had an example using it, but the author also had not made any reference to the tilde before.

var i = 10;
var j = ~i;    // j = -11
var l = ~-i;   // l = 9
var x = ~true; // x = -2
var y = ~false;// y = -1

What is the function of the operator ~ and when it is generally used?

2 answers

38


This is an operator of binary negation, it operates by flipping bit to bit a number.

Simplified example

var x = 4; /* Em binário é representado como 100 */
var y = ~x; /* Agora o x invertido é representado como 011, ou seja, como o número -5,
               devido a negação complemento de 2 */

Using binary operators a 9 turns into 000000000 000000000 00000000 00001001, becoming 11111111 11111111 11111111 11110110, or -10, when denied by the operator NOT binary (~).

Utilizing

Even without much practical utility it is possible to use the operator ~ for downward rounding of positive numbers by denying it twice:

var x = ~~93.4953; //93
var y = ~~94.9999; //94

This is because binary operators transform the type Number, which by default is the type binary64, in 32-bit Signed, which cause the values after the comma to be ignored.

You can find more examples on MDN:

Bitwise Operators

  • Actually in Javascript the type Number is the guy binary64 defined by IEEE 754. The maximum value of an integer in Javascript is 9,007,199,254,740,992, while a 32-bit number Signed, in general terms, is 2,147,483,647.

  • That, I was wrong. They are only considered 32-bit Signed in binary operators. Edited!

  • 5

    Just adding: ~n is equivalent to -n-1, assuming that complement of two is used. And another method (most commonly used) to force the conversion of the number into an integer is to do OR with zero: 93.684|0 -> 93.

18

The operator ~ is the operator NOT binary (Wikipedia, MDN).

Example:

> ~ 5
-6

5 in binary: 0101.

Reversing every bit we have: 1010, that is -6 decimally.

Why -6? Remember the complement of 2!


P.S.

It is important to note that although the maximum integer value of Javascript is 9,007,199,254,740,992 (253), binary operations (bitwise) only work correctly on integers in the 32-bit range.

Ecmascript-262 Edition 5.1:

Some Ecmascript Operators Deal only with integers in the range 231 through 2311 inclusive or in the range 0 through 2321 inclusive. These Operators Accept any value of the Number type but first Convert each such value to one of 232 integer values. See the descriptions of the Toint32 and Touint32 Operators in 9.5 and 9.6, respectively.

This is the case for bitwise operations. So be careful with values that exceed this limit.

> ~ 2147483648
2147483647

The correct result is actually -2147483649 (tested in Firefox 26).

  • I think you have the wrong choice of illustration. This represents a floating point in IEEE754, which has nothing to do with encoding an integer in complement of two.

  • 1

    @Guilhermebernal In JS all numbers are IEEE 754. Integers use at most 52 bits, plus the signal.

  • @bfavaretto Yes, but when applying bitwise operations, they function as if they were integers represented in complement of two, as in this case. The image makes no sense in context.

  • I used the image only to illustrate the complement of 2 in javascript (that exactly what it represents). It may confuse you a bit because in bitwise operations the js will treat them as 32-bit. I removed anyway.

  • Cool, that kills the Math.floor(numero);

Browser other questions tagged

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