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?
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.
– talles
That, I was wrong. They are only considered 32-bit Signed in binary operators. Edited!
– Victor Debone
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
.– Guilherme Bernal