如何:〜运算符? [英] How to: The ~ operator?

查看:230
本文介绍了如何:〜运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能谷歌〜运营商了解更多相关信息。有人可以用简单的词语向我解释它是什么以及如何使用它吗?

I can't google the ~ operator to find out more about it. Can someone please explain to me in simple words what it is for and how to use it?

推荐答案

这是按位NOT

我见过的最常见用法是双位按,用于删除数字的小数部分,例如:

Most common use I've seen is a double bitwise NOT, for removing the decimal part of a number, e.g:

var a = 1.2;
~~a; // 1

为什么不使用 Math.floor ?微不足道的原因是它更快并且使用更少的字节。更重要的原因取决于您希望如何对待负数。考虑:

Why not use Math.floor? The trivial reason is that it is faster and uses fewer bytes. The more important reason depends on how you want to treat negative numbers. Consider:

var a = -1.2;
Math.floor(a); // -2
~~a; // -1

所以,使用 Math.floor 对于向下舍入,请使用 ~~ 切断 (非技术术语)

So, use Math.floor for rounding down, use ~~ for chopping off (not a technical term).

这篇关于如何:〜运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆