按位运算符在 2^31 后停止工作

Bitwise operators stop working after 2^31

本文关键字:停止工作 运算符      更新时间:2023-09-26

>假设我有这个:

// different things you can do
var CAN_EAT = 1,
    CAN_SLEEP = 2,
    CAN_PLAY = 4,
    CAN_DANCE = 8,
    CAN_SWIM = 16,
    CAN_RUN = 32,
    CAN_JUMP = 64,
    CAN_FLY = 128,
    CAN_KILL = 256,
    CAN_BE_JESUS = Math.pow(2, 70);
// the permissions that I have
var MY_PERMS = CAN_EAT | CAN_SLEEP | CAN_PLAY | CAN_BE_JESUS;
// can I eat?
if(MY_PERMS & CAN_EAT) alert('You can eat!'); /* RUNS */
// can I sleep?
if(MY_PERMS & CAN_SLEEP) alert('You can sleep!'); /* RUNS */
// can I play?
if(MY_PERMS & CAN_PLAY) alert('You can play!'); /* RUNS */
// can I be jesus?
if(MY_PERMS & CAN_BE_JESUS) alert('You can be jesus!'); /* WONT RUN */

然后,如果我运行它,它会打印出我可以吃饭,睡觉和玩耍。它不会打印出我可以成为耶稣,因为这个数字是 2^70。如果我输入数字 2^31,那么它将起作用(我在 64 位机器上,但在运行上述示例时必须以 32 位模式运行 Chrome)。

在处理按位运算符时,我也一直在 PHP 中遇到这个问题。通常我可以按照我所处的场景来做到这一点,所以在我的列表中最多有 31 或 63 件事没什么大不了的,但有时我需要更多。有没有办法绕过这个限制?按位运算符非常快速和方便。

好吧,正如您所怀疑的那样,这个问题显然是javascript中整数的宽度。据此,js 中的数字可以达到 2^53,因此您可以有 53 个不同的位。据此,在 64 位机器中,php 一直上升到 2^63 - 1,所以你得到 62 位。
如果你需要更多,你应该重新考虑你的设计 - 你能不能把标志分成2个(或更多)组,每个组都有自己的含义(如与食物相关的动作,其他动作,其他任何东西,等等)?

你可以在 ECMAScript 语言规范中阅读更多关于它的信息,ECMAScript 是 JavaScript 的一个子集,请查看这里和这里。

` Some ECMAScript operators deal only with integers in the range -2^31
through 2^31 - 1, inclusive, or in the range 0 through 2^32-1, inclusive. 
These operators accept any value of the Number type but first convert 
each such value to one of 2^32 integer values. 
See the descriptions of the ToInt32 and ToUint32 operators in 9.5 and 
9.6, respectively. `