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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |