本文翻译自:What is the “double tilde” (~~) operator in JavaScript? [duplicate]
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? ~~
运算符是做什么的?
#1楼
参考:https://stackoom.com/question/P3Ur/什么是JavaScript中的-双波浪号-运算符-重复
#2楼
The diffrence is very simple: 区别很简单:
Long version 长版
If you want to have better readability, use Math.floor
. 如果要提高可读性,请使用Math.floor
。 But if you want to minimize it, use tilde ~~
. 但是,如果要将其最小化,请使用波浪号~~
。
There are a lot of sources on the internet saying Math.floor
is faster, but sometimes ~~
. 互联网上有很多消息来源说Math.floor
更快,但有时~~
。 I would not recommend you think about speed because it is not going to be noticed when running the code. 我不建议您考虑速度,因为在运行代码时不会注意到速度。 Maybe in tests etc, but no human can see a diffrence here. 也许正在测试等中,但是没有人可以看到这里的差异。 What would be faster is to use ~~
for a faster load time. 更快的方法是使用~~
以加快加载时间。
Short version 简洁版本
~~
is shorter/takes less space. ~~
较短/占用较少的空间。 Math.floor
improves the readability. Math.floor
提高了可读性。 Sometimes tilde is faster, sometimes Math.floor
is faster, but it is not noticeable. 有时波浪号速度更快,有时Math.floor
速度更快,但是并不明显。
#3楼
That ~~
is a double NOT bitwise operator. 那~~
是一个双重NOT逐位运算符。
It is used as a faster substitute for Math.floor()
. 它用作Math.floor()
的更快替代品 。
#4楼
~(5.5) // => -6
~(-6) // => 5
~~5.5 // => 5 (same as Math.floor(5.5))
~~(-5.5) // => -5 (NOT the same as Math.floor(-5.5), which would give -6 )
For more info, see: 有关更多信息,请参见:
- http://dreaminginjavascript.wordpress.com/2008/07/04/28/ http://dreaminginjavascript.wordpress.com/2008/07/04/28/
#5楼
It hides the intention of the code. 它隐藏了代码的意图。
It's two single tilde operators, so it does a bitwise complement (bitwise not) twice. 它是两个单个的波浪号运算符,因此它执行了一次按位补码(按位取反)两次。 The operations take out each other, so the only remaining effect is the conversion that is done before the first operator is applied, ie converting the value to an integer number. 这些操作相互抵消,因此唯一剩下的效果是在应用第一个运算符之前进行的转换,即将值转换为整数。
Some use it as a faster alternative to Math.floor
, but the speed difference is not that dramatic, and in most cases it's just micro optimisation. 有些人将其用作Math.floor
的更快替代品,但速度差异并不那么显着,并且在大多数情况下只是微优化。 Unless you have a piece of code that really needs to be optimised, you should use code that descibes what it does instead of code that uses a side effect of a non-operation. 除非您有一段确实需要优化的代码,否则您应该使用描述其功能的代码,而不是使用非操作副作用的代码。
Update 2011-08: 2011-08更新:
With optimisation of the JavaScript engine in browsers, the performance for operators and functions change. 通过优化浏览器中的JavaScript引擎,操作员和函数的性能会发生变化。 With current browsers, using ~~
instead of Math.floor
is somewhat faster in some browsers, and not faster at all in some browsers. 对于当前的浏览器,在某些浏览器中使用~~
代替Math.floor
更快,而在某些浏览器中则根本Math.floor
。 If you really need that extra bit of performance, you would need to write different optimised code for each browser. 如果您确实需要额外的性能,则需要为每个浏览器编写不同的优化代码。
See: tilde vs floor 另请: 波浪线与地板