javascript - 为什么 OR 0 在 Javascript 中舍入数字?

标签 javascript bit-manipulation

我的印象是,根据 IEEE 浮点标准,Javascript 中的 Number 类型可以存储任何数字、整数或 float 。如果是这样,那么为什么要将数字与 0 进行按位或运算将其向下舍入?

尝试使用其他一些位运算,似乎在将位运算应用于 float 时,首先将数字四舍五入为 0,然后应用位运算(数字采用 Two 的补码表示形式,而不是 IEEE)。这是正确的吗?

最佳答案

在 ECMAScript 5.1 中,所有按位运算都会将输入转换为 32 位整数,并返回一个 32 位整数。关于运算符 ^&|section 11.10说:

The production A : A @ B, where @ is one of the bitwise operators in the productions above, is evaluated as follows:

1) Let lref be the result of evaluating A.
2) Let lval be GetValue(lref).
3) Let rref be the result of evaluating B.
4) Let rval be GetValue(rref).
5) Let lnum be ToInt32(lval).
6) Let rnum be ToInt32(rval).
7) Return the result of applying the bitwise operator @ to lnum and rnum. The result is a signed 32 bit integer.

请注意 ToInt32在应用运算符之前应用到两侧。

关于javascript - 为什么 OR 0 在 Javascript 中舍入数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18219441/

相关文章:

javascript - 如何在 ReactJS/Typescript 应用程序中使用 Jest 测试类内的公共(public)异步函数

javascript - 手动编辑日期或清除日期时, Bootstrap 日期选择器更改日期事件不会触发

javascript - 创建闭包和 some_func.bind(this) 之间的性能差异

python - 8 位校验和差一

java - 为什么java按位补码不能正确填充?

algorithm - 带移位算法的最小位串集合并集

javascript - 检测 Jasmine 测试何时完成

javascript - 对关联数组的关联数组执行 foreach

java - 这个按位运算符的实现有何意义?

javascript - 如何将标志写入 7 位数字