javascript - 是否可以将 javascript 数字设置为 32 位?

标签 javascript numbers

是否可以将 javascript 数字设置为 32 位而不是 64 位?

最佳答案

根据 JS 权威指南,如果您进行按位运算,数字会自动转换为 32 位:

The bitwise operators expect integer operands and behave as if those values were represented as 32-bit integers rather than 64-bit floating-point values. These operators convert their operands to numbers, if necessary, and then coerce the numeric values to 32-bit integers by dropping any fractional part and any bits beyond the 32nd. The shift operators require a right-side operand between 0 and 31. After converting this operand to an unsigned 32-bit integer, they drop any bits beyond the 5th, which yields a number in the appropriate range.

您所要做的就是对数字执行按位运算,例如将其与 0 进行或运算。您将得到相同的数字,但它将是 32 位。

关于javascript - 是否可以将 javascript 数字设置为 32 位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23807508/

相关文章:

javascript - return 语句在 setter 中做什么?

vb.net - 如果小于零,则在 VB.net 中强制计算结果等于零

javascript - 如何在 Node js和 Mongoose 中保存用户之前对密码进行哈希处理

javascript - Twitter 的 Bootstrap 固定到顶部的导航栏滚动,页内 anchor 链接跳动

c - 尝试使用整个 rand() 表是个好主意吗?

javascript - 从数字中显示货币

sql - 将 30 到 300 之间的随机数添加到现有字段

java - 在数组中查找多对(两对和/或葫芦)

javascript - 在 javascript 上交换大小写

javascript - 如何使用 Material UI 表分页在每页显示正确的行