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.


In reply to Bitwise & stopped working? by Krokus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.