位运算符改变算术结果

发布于 2024-10-22 19:08:56 字数 309 浏览 1 评论 0原文

有人可以向我解释为什么下面的代码:

  var a = 0xFFFFFFFF;
  a &= 0xFFFFFFFF;
  a += 1;
  alert( "a = " + a );

  var b = 0xFFFFFFFF;
  b += 1;
  alert( "b = " + b );

为 a 和 b 返回不同的值吗?

从 0xFFFFFFFF 开始0xFFFFFFFF 应等于 0xFFFFFFFF,两段代码均应返回 0x100000000。相反,a 获取值 0,b 获取值 0x100000000。

Can somebody explain to me why the following code:

  var a = 0xFFFFFFFF;
  a &= 0xFFFFFFFF;
  a += 1;
  alert( "a = " + a );

  var b = 0xFFFFFFFF;
  b += 1;
  alert( "b = " + b );

returns different values for a and b?

Since 0xFFFFFFFF & 0xFFFFFFFF should equal 0xFFFFFFFF, both pieces of code should return 0x100000000. Instead a get the value of 0 and b get the value of 0x100000000.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

演出会有结束 2024-10-29 19:08:56

JS 按位运算符返回一个有符号 32 位整数。 0xFFFFFFFF 转换为 -1,加 1 得到 0。

JS bitwise operators return a signed 32-bit integer. 0xFFFFFFFF gets converted to -1, and adding 1 gives 0.

铃予 2024-10-29 19:08:56

由于 JavaScript 使用有符号整数,因此 4294967295 无法用 32 位表示,因此它会转换为更宽的类型。

var a = 0xFFFFFFFF;
alert("a = " + a);  // gives 4294967295 (too large for a signed 32-bit integer)

a &= 0xFFFFFFFF;    // gives result as 32 bit signed integer
alert("a = " + a);  // gives -1

有关按位运算符的详细信息,请访问:Mozilla 开发者网络:按位运算符

如果将 a 初始化为 0xFFFFFFF(即 7 F),您将得到预期的结果。

var a = 0xFFFFFFF;
alert("a = " + a);   // gives 268435455
a &= 0xFFFFFFFF;
alert("a = " + a);   // gives 268435455

Since JavaScript works with signed integers, 4294967295 can't be represented in 32 bits, thus it is converted to a wider type.

var a = 0xFFFFFFFF;
alert("a = " + a);  // gives 4294967295 (too large for a signed 32-bit integer)

a &= 0xFFFFFFFF;    // gives result as 32 bit signed integer
alert("a = " + a);  // gives -1

Details on the bitwise operators can be found here: Mozilla Developer Network: Bitwise Operators

If you initialize a to 0xFFFFFFF (thats 7 Fs) you get the expected result.

var a = 0xFFFFFFF;
alert("a = " + a);   // gives 268435455
a &= 0xFFFFFFFF;
alert("a = " + a);   // gives 268435455
烟沫凡尘 2024-10-29 19:08:56

试试这个:

var a = 0xFFFFFFFF;
alert( "a = " + a );
a &= 0xFFFFFFFF;
alert( "a = " + a );
a += 1;
alert( "a = " + a );

应该会更清楚发生了什么。 &= 导致了 b 不会发生的类型转换。

Try this:

var a = 0xFFFFFFFF;
alert( "a = " + a );
a &= 0xFFFFFFFF;
alert( "a = " + a );
a += 1;
alert( "a = " + a );

and it should be a little more apparent what's going on. The &= is causing a type conversion that doesn't happen for b.

季末如歌 2024-10-29 19:08:56

在 JavaScript 中,以下表达式为 true:

(0xFFFFFFFF & 0xFFFFFFFF) == -1

正如 dan04 所说,布尔运算的结果是一个带符号的 32 位整数值,与参数的类型无关。
当您查看 0xFFFFFFFF 和 -1 的位模式时,会发现它们在使用 32 位值时是相同的。

为了始终获得正结果,可以在结果为负时将 2^32 添加到结果中:

function and(a, b) { let y = a & b; return (y < 0) ? (y + 0x100000000) : y;}

则以下表达式为 true

and(0xffffffff, 0xffffffff) == 0xffffffff

In JavaScript, the following expression is true:

(0xFFFFFFFF & 0xFFFFFFFF) == -1

As said by dan04, the result of boolean operations is a signed 32 bit integer value, independent of the type of the parameters.
When you look at the bit-pattern of 0xFFFFFFFF and of -1, they are identical when using 32-bit values.

To always get positive results, you can add 2^32 to the result when it is negative:

function and(a, b) { let y = a & b; return (y < 0) ? (y + 0x100000000) : y;}

Then the following expression is true:

and(0xffffffff, 0xffffffff) == 0xffffffff
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文