我正在使用以下代码在PHP中实现简单的位旋转:
(($n>>1)&0xFFFFFFFF)|(($n&0x00000001)<<31)
当我在不同的机器上运行此代码时,我得到的结果完全不同。起初,我认为这与字节序有关,但是它们都是x86,我只是在使用不同版本的PHP。
是什么原因导致这种行为?
编辑:没关系,我之前的例子不正确
答案 0 :(得分:0)
问题肯定是由PHP_INT_SIZE为4而不是8
引起的。答案 1 :(得分:0)
x86-64窗口上的PHP 5.5和5.6是实验性的,并使用32位算术。没有任何解决办法,因此即使在同一体系结构中,您也需要考虑字长可能会在4到8个字节之间变化。
答案 2 :(得分:0)
您可以在此处逐步检查。我做了一点3v4l.org script
我说明了两个位旋转:第一个是2147483647,第二个是2147483648 第一个是最大32位数字,第二个是另外一个
请参阅:PHP: integers -Manual # Integer overflow
您可以看到,从4.3.0起的每个PHP版本的输出都是相同的
代码如下:
<?php
$n = 2147483647;
shift($n);
echo "\n";
echo "\n";
echo "\n";
echo "\n";
$n = 2147483648;
shift($n);
function out($a, $b, $c, $b2 = '0', $c1 = ' ') {
$padstra = 40;
$padstrb = 23;
echo
str_pad($a, $padstra, ' ', STR_PAD_LEFT)
. " "
. str_pad(decbin($b), 32, $b2, STR_PAD_LEFT)
. " "
. str_pad($c, $padstrb, $c1) . "\n";
}
function shift($n) {
out($n , $n , '$n' );
echo "\n";
out('A:', $n >> 1 , '$n >> 1' );
out('B:', 0xFFFFFFFF , '0xFFFFFFFF' );
out('A & B:', ($n >> 1) & 0xFFFFFFFF , '($n >> 1) & 0xFFFFFFFF' );
echo "\n";
out('C:', $n , '$n' );
out('D:', 0x00000001 , '0x00000001' );
out('C & D:', $n & 0x00000001 , '$n & 0x00000001' );
echo "\n";
out('C & D:', $n & 0x00000001 , '$n & 0x00000001' );
out('(C & D) << 31:', ($n & 0x00000001) << 31 , '($n & 0x00000001) << 31' );
echo "\n";
out('A & B:', ($n >> 1) & 0xFFFFFFFF , '($n >> 1) & 0xFFFFFFFF' );
out('(C & D) << 31:', ($n & 0x00000001) << 31 , '($n & 0x00000001) << 31' );
out('(A & B) | ((C & D) << 31):', ($n >> 1) & 0xFFFFFFFF | ($n & 0x00000001) << 31 , '($n >> 1) & 0xFFFFFFFF | ($n & 0x00000001) << 31');
}
这是输出
2147483647 01111111111111111111111111111111 $n
A: 00111111111111111111111111111111 $n >> 1
B: 11111111111111111111111111111111 0xFFFFFFFF
A & B: 00111111111111111111111111111111 ($n >> 1) & 0xFFFFFFFF
C: 01111111111111111111111111111111 $n
D: 00000000000000000000000000000001 0x00000001
C & D: 00000000000000000000000000000001 $n & 0x00000001
C & D: 00000000000000000000000000000001 $n & 0x00000001
(C & D) << 31: 10000000000000000000000000000000 ($n & 0x00000001) << 31
A & B: 00111111111111111111111111111111 ($n >> 1) & 0xFFFFFFFF
(C & D) << 31: 10000000000000000000000000000000 ($n & 0x00000001) << 31
(A & B) | ((C & D) << 31): 10111111111111111111111111111111 ($n >> 1) & 0xFFFFFFFF | ($n & 0x00000001) << 31
2147483648 10000000000000000000000000000000 $n
A: 01000000000000000000000000000000 $n >> 1
B: 11111111111111111111111111111111 0xFFFFFFFF
A & B: 01000000000000000000000000000000 ($n >> 1) & 0xFFFFFFFF
C: 10000000000000000000000000000000 $n
D: 00000000000000000000000000000001 0x00000001
C & D: 00000000000000000000000000000000 $n & 0x00000001
C & D: 00000000000000000000000000000000 $n & 0x00000001
(C & D) << 31: 00000000000000000000000000000000 ($n & 0x00000001) << 31
A & B: 01000000000000000000000000000000 ($n >> 1) & 0xFFFFFFFF
(C & D) << 31: 00000000000000000000000000000000 ($n & 0x00000001) << 31
(A & B) | ((C & D) << 31): 01000000000000000000000000000000 ($n >> 1) & 0xFFFFFFFF | ($n & 0x00000001) << 31