本文翻译自:What is the “double tilde” (~~) operator in JavaScript? [duplicate]

This question already has answers here : 这个问题已经在这里有了答案 :
What does ~~ (“double tilde”) do in Javascript? Java中~~(“双波浪号”)有什么作用? (9 answers) (9个答案)
Closed 6 years ago . 6年前关闭。

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 另请: 波浪线与地板

什么是JavaScript中的“双波浪号”(~~)运算符? [重复]相关推荐

  1. (转载)JavaScript:双波浪号“~~“ 与 Math.floor()

    [ 问题描述 ] 在网上看到了两种设置随机颜色的取值代码.但不知道"~~"是干什么用的? var a = (~~(Math.random() * 255)); //"~~ ...

  2. JavaScript:双波浪号“~~“ 与 Math.floor()

    [ 问题描述 ] 在网上看到了两种设置随机颜色的取值代码.但不知道"~~"是干什么用的? var a = (~~(Math.random() * 255)); //"~~ ...

  3. c++ 中. 和 -,波浪号 ~ 符号怎么用 ————很重要

    参考:https://www.cnblogs.com/Simulation-Campus/p/8809999.html 1.  用在类中的析构函数之前,表示该函数是析构函数.如类A的析构函数 clas ...

  4. JavaScript中几个优雅的运算符使用技巧

    ECMAScript发展进程中,会有很多功能的更新,比如销毁,箭头功能,模块,它们极大的改变JavaScript编写方式,可能有些人喜欢,有些人不喜欢,但像每个新功能一样,我们最终会习惯它们.新版本的 ...

  5. c++如何将int数组中的值取出*号运算符_如何用动态规划巧妙解决 “双十一” 购物时的凑单问题?羊毛薅起来!!!...

    点击上方"程序员大白",选择"星标"公众号 重磅干货,第一时间送达 今年过去的 "双十一" ,你有薅到羊毛吗? 每年的双十一,会有各种促销活 ...

  6. Linux中$home和波浪号~

    在Linux中当前用户的主目录可以有三种表示方法,都是等效的[正常不改配置情况下] 例如用户名为sy,进入当前用户的主目录/home/sy 输入cd ~ 或输入cd $HOME 或输入cd /home ...

  7. javascript中基本包装、算数运算符、递增递减运算符、赋值运算符、比较运算符、逻辑运算符、运算符优先级

    基本包装类型: 指基本类型的数据变量通过调用属性或者方法包装成了复杂类型,这个变量也称为基本包装类型对象 <script>var str = 'hello';str = str.repla ...

  8. linux中波浪号代表什么_【整理】Linux系统中用波浪号~表示用户的根目录即$HOME,以及为何用波浪号表示用户根目录...

    背景 Linux世界中,常常看到波浪号,去表示用户根目录. 但是背后的深意和原因,一直不知道. 所以需要去总结搞清楚. Linux中用波浪号~表示$HOME即用户根目录 Linux中,在命令行操作时, ...

  9. bash之波浪号扩展(tilde expansion)

    写在前面 对于使用过linux的人来说,对于波浪号扩展应该不会陌生,都知道~号代表的是当前用户的宿主目录. 但是呢,我想说的是,对于波浪号扩展的认知了解到这个程度可能还不够,波浪号扩展还有其它的意义. ...

最新文章

  1. 如何高效便捷的画出炫酷神经网络图
  2. 海思3536:PC客户端编译过程报错及解决方法
  3. Python赋值运算符(入门必读)
  4. Log4j2的性能为什么这么好?
  5. 2019.1.7 区块链论文翻译
  6. ipython源代码怎么打开_CentOS 6.4 中IPython如何启动Qt控制台和NoteBook?
  7. MAC--PPTP教程
  8. Spring boot Mybatis 整合(注解版)
  9. RHEL7/CentOS7 Network Service开机无法启动的解决方法
  10. Filter的基本配置
  11. 深入剖析Android音频之AudioPolicyService
  12. 计算机组成原理-概述篇
  13. 时间序列分析实验报告总结_时间序列分析实验报告
  14. troublemaker中文谐音_trouble maker的中文音译歌词 希望准确的遵循原来的发音。。。不是中文歌词和韩语歌词!要的是音译啊 注意...
  15. 测试总结报告写法简单总结
  16. 比起高性能计算,高端存储才更亟待国产化
  17. matlab逆变器原理,MATLAB中的单相全桥逆变器电路建模与仿真
  18. DNS(域名系统)是什么?
  19. java 读取ascii码_JAVA ASCII码 读入字符串测试
  20. 前后端分离基于Oauth2的SSO单点登录怎样做?

热门文章

  1. Python 定时执行:schedule 模块
  2. postgresql 自动创建分区表
  3. 全国计算机等级考试 考务会,3月24-26日:2018年上半年全国计算机等级考试开始!...
  4. 如何治理资源浪费?百度云原生成本优化最佳实践
  5. Java精品源码小程序第6期超豪华在线点餐系统
  6. 缘灭--HashMap底层原理之1.8put源码篇(三)
  7. emwin 5.5下载
  8. Button..::.CausesValidation 属性
  9. Unable to preventDefault inside passive event listener
  10. 嵌入式开发环境搭建 与 系统移植