PHP中对大整数的按位运算的最大限制是什么?
我刚刚意识到一些有用的东西,希望与大家分享: 我在64位Windows7机器上运行PHPX86。我试着做一个权限系统,我有这样一句话:PHP中对大整数的按位运算的最大限制是什么?,php,bit-manipulation,Php,Bit Manipulation,我刚刚意识到一些有用的东西,希望与大家分享: 我在64位Windows7机器上运行PHPX86。我试着做一个权限系统,我有这样一句话: // value of $role came from database i.e. 0xffffffff // value of $function_ACL is hardcoded in PHP file using 32bit hex notation i.e. 0x80000000 // true if access is allowed return (
// value of $role came from database i.e. 0xffffffff
// value of $function_ACL is hardcoded in PHP file using 32bit hex notation i.e. 0x80000000
// true if access is allowed
return ($function_ACL & $role) != 0
不知何故,$role
的值使用的规则进行转换,从而达到整数限制,结果是错误的0
为了解决这个问题,我注意到我们可以这样做
$function_ACL += 0;
$role += 0;
return ($function_ACL & $role) != 0 // works! which is odd, because type conversion don't follow the same routine
这让我想知道真正的限制是什么,然后试验了几个非常大的数字
// these numbers get converted to scientific notation
echo 0xffffffffffffffffffffffffffffffff;
echo 9999999999999999999999999999999999
// 52 bits (13 f's) is the max limit for a correct bitwise operation
echo (0xfffffffffffff & 0x0000000000001);
有人有更多的贡献吗?看看。它允许对大数进行位运算
$role = gmp_init("0xffffffff");
$function_ACL = gmp_init("0x80000000");
if (gmp_and($role, $function_ACL))
echo "yes!";