Bitwise And only works on integers stored as integers (or on strings), not on floats. You're generating a number that doesn't fit in an integer, so Perl switches to using floats.

On a 32-bit Perl, doing & 0xffffffff will do anything at best, produce garbage at worse.

If you want to rely on the fact that Perl actually gives you at least 52 bits of precision for unsigned numbers through the use of floats, you could use division as an alternative.

$hash = ($c + ($hash * 64) + ($hash * 65536) - $hash); $hash -= int($hash/2**32)*2**32;

I haven't checked to see if this is safe for every unsigned 32-bit $c and every unsigned 32-bit $hash.
It's safe for every unsigned 32-bit $c and every unsigned 32-bit $hash.

Update: The above is a silly way of writing

$hash = ($c + ($hash * 64) + ($hash * 65536) - $hash) % 2**32;

I thought Perl didn't have a mod operator.


In reply to Re: Bitwise & stopped working? by ikegami
in thread 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.