hash() yields different results for different platforms
Tim Peters
tim.peters at gmail.com
Wed Jul 12 03:06:19 EDT 2006
[Grant Edwards]
>> ...
>> The low 32 bits match, so perhaps you should just use that
>> portion of the returned hash?
>>
>> >>> hex(12416037344)
>> '0x2E40DB1E0L'
>> >>> hex(-468864544 & 0xffffffffffffffff)
>> '0xFFFFFFFFE40DB1E0L'
>>
>> >>> hex(12416037344 & 0xffffffff)
>> '0xE40DB1E0L'
>> >>> hex(-468864544 & 0xffffffff)
>> '0xE40DB1E0L'
[Qiangning Hong]
> Is this relationship (same low 32 bits) guaranteed?
No. Nothing about hashes is guaranteed, except that when x and y are
of a hashable type, and x == y, then hash(x) == hash(y) too.
> Will it change in the future version?
That's possible, but not planned. Note that the guts of string
hashing in CPython today is implemented via
while (--len >= 0)
x = (1000003*x) ^ *p++;
where x is C type "long", and the C language doesn't even define what
that does (behavior when signed multiplication overflows isn't defined
in C).
More information about the Python-list
mailing list