32 位上的 gmp_xor 问题

发布于 2024-11-26 12:16:09 字数 362 浏览 5 评论 0原文

在 64 位机器上

(1 ^ 2489596804)

给出 -1805370491 作为响应。但是,当我在 32 位 PHP 机器上执行此操作时,我得到 2489596805 响应。

因此,我尝试使用 gmp_strval(gmp_xor(1,2489596804)) 在 32 位计算机上运行它,但它也会产生 2489596805。那么到底是怎么回事,我怎样才能得到正确的回应呢?

更新:我刚刚注意到 64 位结果是从中减去 32 位最大值所得的结果 (4294967295)。

On a 64-bit machine

(1 ^ 2489596804)

gives -1805370491 in response. However, when I do this on a 32-bit PHP machine, I get 2489596805 in response.

So, I tried to get it working on the 32-bit machine using gmp_strval(gmp_xor(1,2489596804)), but it also yields 2489596805. So what's up, and how can I get the right response?

Update: I just noticed that the 64-bit result is what you get from subtracting the 32-bit max from it (4294967295).

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

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

发布评论

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

评论(1

幻想少年梦 2024-12-03 12:16:09

这是相同的结果,只是解释不同:

0x94643B85 =  2489596805 (unsigned 32-bit integer)
0x94643B85 = -1805370491 (signed 32-bit integer)

您在 64 位计算机上看到负值的原因是您将该结果解释为带符号的 32 位整数。

如果你真的想在两种解释之间进行转换,我想你必须自己做。一个简单的方法是这样的:

<?php
function toSigned($value) {
  if ($value <= 2147483647)
    return $value;
  return $value - 4294967295 + 1;
}

function toUnsigned($value) {
  if ($value >= 0)
    return $value;
  return $value + 4294967295 - 1;
}

It's the same result, simply interpreted differently:

0x94643B85 =  2489596805 (unsigned 32-bit integer)
0x94643B85 = -1805370491 (signed 32-bit integer)

The reason you see a negative value on your 64-bit machine is that you're interpreting that result as a signed 32-bit integer.

If you really want to convert between the two interpretations, I think you'll have to do this yourself. A simple way to do that would be something like this:

<?php
function toSigned($value) {
  if ($value <= 2147483647)
    return $value;
  return $value - 4294967295 + 1;
}

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