hash() yields different results for different platforms

Qiangning Hong hongqn at gmail.com
Wed Jul 12 02:52:06 EDT 2006


Grant Edwards wrote:
> On 2006-07-11, Qiangning Hong <hongqn at gmail.com> wrote:
> > However, when I come to Python's builtin hash() function, I
> > found it produces different values in my two computers!  In a
> > pentium4, hash('a') -> -468864544; in a amd64, hash('a') ->
> > 12416037344.  Does hash function depend on machine's word
> > length?
>
> Apparently. :)
>
> 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'

Is this relationship (same low 32 bits) guaranteed?  Will it change in
the future version?




More information about the Python-list mailing list