• JavaScript默认32位,32位操作系统int类型的最大值是 2147483647,超过该数值会溢出
  • php 64位需要使用pack转化

PHP中pack、unpack的详细用法:

  • JavaScript运行结果
php和js位移运算的坑
php和js位移运算的坑
  • PHP运行结果
php和js位移运算的坑
php和js位移运算的坑

JavaScript

1
2
3
4
5
function (t) hash33{
for (var e = 0, i = 0, n = t . length; i < n; ++i)
e += (e << 5) + t . charCodeAt(i);
return 2147483647 & e
}

PHP

1
2
3
4
5
6
7
8
function hash33($t){
for ( $e = 0, $i = 0, $n = strlen($t); $i < $n; ++$i){
$tmp = $e << 5;
$tmp = unpack("L",pack("L",$tmp))[1];
$e += $tmp+ uniord(mb_substr($t,$i,1,'utf-8'));
}
return 2147483647 & $e;
}