[San-Diego-pm] CRC32, bitwise operators, and PHP

Chris Radcliff chris_radcliff at mac.com
Tue Apr 17 06:52:28 PDT 2007

Hi Mongers,

I've got a stumper today.  Perhaps one of you can help.  We're doing  
a PHP-to-Perl transition of some of our code, and it requires both  
environments to access the same keys from a memcached server.  The  
key hashing algorithm uses CRC32 and a few bit shifts, and that's  
beyond my PHP knowledge (and frankly on the edge of my Perl skill, too.)

The code in Perl is the fixed part.  Here's a simplified test:

use String::CRC32;

my $key = "foo";
print (crc32($key) >> 16) & 0x7fff;
print "\n";

# perl foo.pl
# 35955

The PHP code should be changed to match.  We hoped that the same  
syntax would work, but unfortunately the results are different:

$key = "foo";
print  (crc32($key) >> 16) & 0x7fff;
print "\n";

# php foo.php
# 3187

Any bit-shifting wizards out there willing to take a crack at this?   
It looks like it should be a simple problem,  but the vagaries of  
cross-language bitwise operator differences are hard to dig up.

Let me know if processor architecture matters here, or operating  
system.  These particular tests were run on a PPC Mac, but the target  
environment is X86 running Fedora Core 2.  Let me know if I'm making  
any other incorrect assumptions; that'll at least give me something  
to track down.


More information about the San-Diego-pm mailing list