Re: how to allow integer overflow for calculating hash code of a string?

From: Craig James <cjames(at)emolecules(dot)com>
To: Haifeng Liu <liuhaifeng(at)live(dot)com>
Cc: "pgsql-admin(at)postgresql(dot)org" <pgsql-admin(at)postgresql(dot)org>
Subject: Re: how to allow integer overflow for calculating hash code of a string?
Date: 2012-09-20 14:34:29
Message-ID: CAFwQ8rcdk68h5kC+77VdPqwPmju3KD1BTkm6Sh4_YYEzcBwcwA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Thu, Sep 20, 2012 at 1:55 AM, Haifeng Liu <liuhaifeng(at)live(dot)com> wrote:

> I want to write a hash function which acts as String.hashCode() in java:
> hash = hash * 31 + s.charAt(i)... but I got integer out of range error. How
> can I avoid this? I saw java do not care overflow of int, it just make the
> result negative.
>
>
Use the bitwise AND operator to mask the hash value with 0x3FFFFFF before
each iteration:

hash = (hash & 67108863) * 31 + s.charAt(i);

Craig

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Kasia Tuszynska 2012-09-20 17:01:15 Backup and Restore from 8.3.0 to 9.1.3
Previous Message Haifeng Liu 2012-09-20 08:55:46 how to allow integer overflow for calculating hash code of a string?