Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "double tilde" (~~) operator in JavaScript? [duplicate]

Tags:

javascript

I'm seeing this in some code, and I have no idea what it does:

var jdn = function(y, m, d) {   var tmp = (m <= 2 ? -1 : 0);   return ~~((1461 * (y + 4800 + tmp)) / 4) +           ~~((367 * (m - 2 - 12 * tmp)) / 12) -           ~~((3 * ((y + 4900 + tmp) / 100)) / 4) +           d - 2483620; }; 

What's the ~~ operator do?

like image 330
jismo Avatar asked May 11 '11 23:05

jismo


People also ask

What is double tilde in JavaScript?

The “double tilde” (~~) operator is a double NOT Bitwise operator. Use it as a substitute for Math. floor(), since it's faster.

What does a double tilde mean?

Another approximation symbol is the double tilde ≈, meaning "approximately equal to". The tilde is also used to indicate congruence of shapes by placing it over an = symbol, thus ≅.

What is tilde operator in JavaScript?

The (~) tilde operator takes any number and inverts the binary digits, for example, if the number is (100111) after inversion it would be (011000).

What does double tilde mean in R?

Tilde is a R's "Primitive Function" that does not evaluate its argument, and it is normally used to create a formula object as an inner-DSL role. I hijack this functionality to make an anounymous function. Double-tilde with a two-dots symbol, .. , makes an anonymous function in which two-dots plays a placeholder.


1 Answers

That ~~ is a double NOT bitwise operator.

It is used as a faster substitute for Math.floor() for positive numbers. It does not return the same result as Math.floor() for negative numbers, as it just chops off the part after the decimal (see other answers for examples of this).

like image 118
ghoppe Avatar answered Oct 23 '22 10:10

ghoppe