Home  >  Article  >  Backend Development  >  关于php中intval在32和64位的有关问题

关于php中intval在32和64位的有关问题

WBOY
WBOYOriginal
2016-06-13 12:50:471229browse

关于php中intval在32和64位的问题

php的int行在32位的系统上是4Byte,在64位上是8Byte,导致在超过4Byte的整数,在32和64返回的结果不同,现在写一个统一的函数,统一用32位的算法


function intval32($num) {
 $num = $num & 0xffffffff;//消掉高32位  
 
$p = $num>>31; //取第一位 判断是正数还是负数
 if($p==1) { //负数
 $num = $num-1;
 $num = ~$num; //取反 会当成64位取反,算出来的数就去了,所以取反之后 要消掉 高32位
 $num = $num & 0xffffffff;
 return $num * -1;
 } else {
 return $num;
 }
 } 



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn