为什么js里面对浮点数两次按位取反可以得到整数?

如何证明
~~(1.2) === parseInt("1.2",10);// true

阅读 3k
1 个回答

取反操作符会将Number类型变量转换为整型。而一个整数取反两次则显然是自身。

6.1.6.1.2 Number::bitwiseNOT ( x )
The abstract operation Number::bitwiseNOT takes argument x (a Number) and returns an integral Number. It performs the following steps when called:

  1. Let oldValue be ! ToInt32(x).
  2. Return the result of applying bitwise complement to oldValue. The mathematical value of the result is exactly representable as a 32-bit two's complement bit string.

https://tc39.es/ecma262/multi...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题