JavaScript 中的“双波浪号”(~~)运算符是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5971645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:43:20  来源:igfitidea点击:

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

javascript

提问by jismo

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?

~~运营商是做什么的?

回答by ghoppe

That ~~is a double NOT bitwise operator.

~~是一个双非按位运算符。

It is used as a faster substitute for Math.floor().

它用作 的更快替代品Math.floor()

回答by Guffa

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, i.e. 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. 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.

有些人将其用作 的更快替代品Math.floor,但速度差异并不那么显着,在大多数情况下,这只是微优化。除非您有一段真正需要优化的代码,否则您应该使用描述其功能的代码,而不是使用非操作副作用的代码。

Update 2011-08:

2011-08 更新:

With optimisation of the JavaScript engine in browsers, the performance for operators and functions change. With current browsers, using ~~instead of Math.flooris somewhat faster in some browsers, and not faster at all in some browsers. If you really need that extra bit of performance, you would need to write different optimised code for each browser.

随着浏览器中 JavaScript 引擎的优化,运算符和函数的性能发生了变化。对于当前的浏览器,在某些浏览器中使用~~而不是Math.floor更快一些,而在某些浏览器中则根本不快。如果您真的需要额外的性能,则需要为每个浏览器编写不同的优化代码。

See: tilde vs floor

请参阅:波浪号与地板

回答by bowsersenior

~(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:

有关更多信息,请参阅:

回答by Jason Stackhouse

The diffrence is very simple:

区别很简单:

Long version

长版

If you want to have better readability, use Math.floor. But if you want to minimize it, use tilde ~~.

如果您想获得更好的可读性,请使用Math.floor. 但是如果你想最小化它,使用波浪号~~

There are a lot of sources on the internet saying Math.flooris faster, but sometimes ~~. 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.

互联网上有很多来源说Math.floor更快,但有时~~。我不建议您考虑速度,因为在运行代码时不会注意到它。也许在测试等中,但没有人可以看到这里的差异。更快的是使用~~更快的加载时间。

Short version

精简版

~~is shorter/takes less space. Math.floorimproves the readability. Sometimes tilde is faster, sometimes Math.flooris faster, but it is not noticeable.

~~更短/占用更少的空间。Math.floor提高了可读性。有时波浪号更快,有时Math.floor更快,但并不明显。