Krokus has asked for the wisdom of the Perl Monks concerning the following question:
This is driving me mad. I have a perl script that has been working for several months but the other day it broke (the script hasn't been modified since I first wrote it). At first I thought, "well, it must be a result of something I did elsewhere that's mucking up the input to the script", but when I looked closer I realized that a bitwise & was now failing to work properly.
In the script I have a function that hashes a string to a 32-bit value. It looks like this:
sub MakeHashString($) { my $string = shift; my $hash = 0; my $i = 0; my $c = 0; for ($i = 0; $i < length($string); ++$i) { $c = ord(substr($string, $i, 1)); $hash = ($c + ($hash * 64) + ($hash * 65536) - $hash) & 0xfffff +fff; } return $hash; }
I added prints to the function to isolate the problem. When this function gets called with, for example, the string "accept", here is what $c and $hash are at the end of each loop iteration, showing $hash before and after the "& 0xffffffff":
c = 97, hash = 97 masked hash = 97 c = 99, hash = 6363202 masked hash = 6363202 c = 99, hash = 417419688097 masked hash = 4294967295 c = 101, hash = 281745559584806 masked hash = 4294967295 c = 112, hash = 281745559584817 masked hash = 4294967295 c = 116, hash = 281745559584821 masked hash = 4294967295
After the third iteration, $hash exceeds 0xffffffff and is forevermore relegated to being masked to a result of 0xffffffff. It doesn't matter if I mask it with the base 10 literal instead (i.e., 4294967295).
As I said, this script has worked fine for ages. My only thought was that perhaps my system path got messed up and a different version of perl is running. The version of perl I'm running which produces this issue is:
This is perl, v5.8.8 built for MSWin32-x86-multi-thread (with 50 registered patches, see perl -V for more detail) Copyright 1987-2006, Larry Wall
Anyone have an idea as to why bitwise & would fail like this?
Update: The perl version is ActiveState's build.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Bitwise & stopped working?
by ikegami (Patriarch) on May 12, 2010 at 16:51 UTC | |
|
Re: Bitwise & stopped working?
by moritz (Cardinal) on May 12, 2010 at 16:51 UTC | |
by Krokus (Novice) on May 12, 2010 at 19:01 UTC | |
by proceng (Scribe) on May 12, 2010 at 22:44 UTC | |
by Krokus (Novice) on May 14, 2010 at 04:46 UTC | |
by ikegami (Patriarch) on May 12, 2010 at 17:05 UTC | |
|
Re: Bitwise & stopped working?
by JavaFan (Canon) on May 12, 2010 at 16:41 UTC | |
by kennethk (Abbot) on May 12, 2010 at 16:50 UTC | |
by ikegami (Patriarch) on May 12, 2010 at 16:58 UTC | |
|
Re: Bitwise & stopped working?
by ambrus (Abbot) on May 14, 2010 at 10:35 UTC |