I came across this implementation of PJW and it seems incorrect:
h = ( h << 4 ) + ( k );
if( ( g = ( h & 0xf0000000 ) ) != 0 )
{
h = ( h ^ ( g >> 24 ) ) ^ g;
}
In a more readable form:
h = ( h << 4 ) + k;
g = h & 0xf0000000;
if( g != 0 )
h = h ^ ( g >> 24 ) ^ g;
(Syntax: <<
and >>
are bit shift operators, &
is bitwise and, |
is bitwise or, ^
is bitwise xor, ~
is bitwise negation, =
is assignment, !=
is not-equal.)
Should it resemble this instead?
h = ( h << 4 ) + s;
s = s + 1;
high = h & 0xF0000000;
if ( high != 0 )
h = h ^ (high >> 24);
h = h & ~high;
A DrDobbs article says "Unfortunately, Holub's version contains an error that was picked up in subsequent texts (including the first printing of Practical Algorithms for Programmers, by John Rex and myself). The bug does not prevent the code from working, it simply gives suboptimal hashes." and I'm wondering if the difference between the top and bottom (edited) is that misprint.