简体   繁体   English

如何在 PHP 的 64 位安装上使用 32 位 integer?

[英]How to use a 32bit integer on a 64bit installation of PHP?

I run into a problem on my code when moved to production because the production server is 32bit while my development machine is 64bit (I'm running Kubuntu 12.04 64bit).移至生产环境时,我的代码遇到了问题,因为生产服务器是 32 位,而我的开发机器是 64 位(我运行的是 Kubuntu 12.04 64 位)。 My question is.我的问题是。 Is it possible to force an int to be 32bit without installing the 23bit version of PHP?是否可以在不安装 PHP 的 23 位版本的情况下将 int 强制为 32 位? Either that or a library that allow me to chose the max int value那个或允许我选择最大 int 值的库

Integers are the size of pointers on that platform. 整数是该平台上指针的大小。 (32-bit PHP --> 32-bit integers. 64-bit PHP --> 64-bit integers). (32位PHP - > 32位整数.64位PHP - > 64位整数)。

Note that when integer operations overflow, the variables become floats. 请注意,当整数运算溢出时,变量变为浮点数。 The PHP documentation explains all of this well. PHP文档很好地解释了所有这些。

I'm not sure what you're doing in your code that would cause you to care what size the integer is though. 我不确定你在代码中做了什么会导致你关心整数的大小。 If you only care about 32-bits of a value, however, you can always mask off the low 32 bits: 但是,如果您只关心32位的值,则可以始终屏蔽低32位:

$val = $something & 0xFFFFFFFF;
$r = $r & 0xFFFFFFFF;
if ($r & 0x80000000)
{
    $r = $r & ~0x80000000;
    $r = -2147483648 + $r;
}
 $r = $r & 0xFFFFFFFF; if ($r & 0x80000000) { $r = $r & ~0x80000000; $r = -2147483648 + $r; }

Trying to give an explanation for who requested it.试图解释谁要求它。

Take your $r big number variable (let's say spread on 64 bits) and let only the 32 most right bits to pass.取你的 $r 大数字变量(假设在 64 位上展开),只让最右边的 32 位通过。

$r = $r & 0xFFFFFFFF; //aka 0x00000000FFFFFFFF

Now 64bits $r variable has 32 zeros and your 32 bits.现在 64 位 $r 变量有 32 个零和你的 32 位。 Note that conventionally the most left bit is the sign, in both 64 and 32 bits representation: plus (0) or minus (1), so 64bits $r is now considered positive by PHP.请注意,通常最左边的位是符号,在 64 位和 32 位表示中:加号 (0) 或减号 (1),因此 PHP 现在将 64 位 $r 视为正数。

if ($r & 0x80000000) {

" 32 zeros, your 32 bits " ANDed with " 32 zeros, 1 and 31 zeros ": if this gives true, meaning there is at least (at most) one 1, which again means: "in the 32bit world, this is a negative number", in this case then we need to also tell PHP that this is a negative number, because most left bit of the 64bits $r variable is still 0 and PHP still thinks is a positive number. 32 个零,你的 32 位”与“ 32 个零,1 和 31 个零”进行 AND 运算:如果这为真,则意味着至少(至多)有一个 1,这再次意味着:“在 32 位世界中,这是一个负数”,在这种情况下,我们还需要告诉 PHP 这是一个负数,因为 64 位 $r 变量最左边的位仍然是 0,而 PHP 仍然认为是一个正数。

So, let's tell PHP this is actually a negative number and rebase the number.所以,让我们告诉 PHP 这实际上是一个负数并重新设置该数字的基数。 First step, set to zero just the most left of your 32 bits to force the positiveness of your 32bits.第一步,将 32 位的最左边设置为零,以强制 32 位为正。

$r = $r & ~0x80000000;

~0x80000000 indeed can be rewritten as 0xFFFFFFFF7FFFFFFF: 32 ones, 1 zero, 31 ones. ~0x80000000 确实可以重写为 0xFFFFFFFF7FFFFFFF:32 个,1 个零,31 个。

Now you have a positive 32bit number representation inside a 64bit variable and it is complementary of it's related 32bit negative representation: eg if you had 64 ones in the very beginning (-1 in decimal), at this point $r is 32 zeros, 1 zero you just set, 31 ones (2147483647 in decimal).现在你在 64 位变量中有一个正的 32 位数字表示,它是它相关的 32 位负数表示的补充:例如,如果你一开始有 64 个(十进制 -1),此时 $r 是 32 个零,1您刚刚设置的零,31 个(十进制为 2147483647)。

You should now see the point:你现在应该明白重点了:

$r = -2147483648 + $r;

Minimum representable 32bit number + our complementary positive number.最小可表示的 32 位数字 + 我们的互补正数。

At this point, $r variable is still a 64bit variable, but it now correctly represents the negativiness of the 32 most right bits you chose in the beginning.此时,$r 变量仍然是一个 64 位变量,但它现在正确地表示了您在开始时选择的最右边的 32 位的负数。

The whole problem is that in 64bit PHP, if you extract the most right 32 bits and you fill another 64bits variable with, the value is still positive because the most left of the 64bits is 0. What we wanted to achieve instead: making the 64bits variable negative, if the extracted 32bits represent a negative number;整个问题是,在 64 位 PHP 中,如果您提取最右边的 32 位并填充另一个 64 位变量,该值仍然是正数,因为 64 位的最左边是 0。我们想要实现的是:制作 64 位变量negative,如果提取的32bits代表一个负数; a 64bits variable positive, if the extracted 32bits represent a positive number.一个 64 位变量正数,如果提取的 32 位表示一个正数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM