PHP hash function compatible with 32-bit and 64-bit machines _php Tutorial

Source: Internet
Author: User
Times in progress, the development of society, the emergence of 64-bit machine is no longer easy to overflow, this is certainly a happy thing, can be in the system migration, suddenly found that 64-bit machine hash function results with 32 is not the same, shocked to quickly study the algorithm in the discovery is the problem of shift operation symbol.
No way, had to judge the system, and for 64-bit machine rewrite the algorithm, the code is as follows:

if (EXEC ("getconf long_bit") = = "+") {function _hash ($apszData = "", $MAX _table_num = 0) {  $_hash = $x = $i = 0;
  
   for ($i = 0; $i < strlen ($apszData); $i + +) {   $_hash = ($_hash << 4) + ord ($apszData [$i]);   if ($x = $_hash &0xf0000000)! = 0) {    $_hash ^= ($x >>);    $_hash &= ~ $x;   }  }  Return ($_hash &0x7fffffff)% $MAX _table_num + 1; }} else {function _hash ($apszData = "", $MAX _table_num = 0) {  $hash = $x = $i = 0;  for ($i = 0; $i < strlen ($apszData); $i + +) {   $hash = ($hash << 4) + ((Ord ($apszData [$i])) << +);    if ($x = $hash &0xf000000000000000)! = 0) {    $hash ^= $x >>;    $hash &= (~ $x) &0xFFFFFFFF00000000;   }  }   $ret = ($hash &0x7fffffff00000000)% ($MAX _table_num << +) + (1 << +);  $ret >>=;  $ret &= 0xFFFFFFFF;  return $ret; }}
  

http://www.bkjia.com/PHPjc/735127.html www.bkjia.com true http://www.bkjia.com/PHPjc/735127.html techarticle times in progress, the development of society, the emergence of 64-bit machine is no longer easy to overflow, this is a happy thing, can be in the system migration, suddenly found 64-bit machine ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.