gpt4 book ai didi

javascript - 如何找到数字的 32 位

转载 作者:行者123 更新时间:2023-11-29 17:28:51 25 4
gpt4 key购买 nike

我想问一下如何找到数字的 32 位版本,因为我想在 JavaScript 中使用按位 AND 运算符处理数字。它表示数字在 32 位版本中执行按位运算。

第二个问题是JavaScript中的按位AND运算符(&),数字的运算在32位版本中执行,对吧?然后最后它是否将其转换回 64 位版本?

最佳答案

根据 the ECMAScript specification ,按位运算的返回值必须是 32 位整数。相关引用:

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.

因此,要将任何数字转换为 32 位整数,您可以只执行二进制运算,但不会产生任何影响。例如,我在这里使用空操作二进制或 (| 0) 将 float 转换为整数:

var x = 1.2, y = 1
x = x | 0
alert(x == y) # prints "true"

关于javascript - 如何找到数字的 32 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6319767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com