64Bit & 32Bit HashCode

以下为64位和32位的hash值计算方法
public class HashUtil {

    private static final long FNV_64_INIT = 0xcbf29ce484222325L;
    private static final long FNV_64_PRIME = 0x100000001b3L;

    private static final long FNV_32_INIT = 2166136261L;
    private static final long FNV_32_PRIME = 16777619;

    public static Long getUnsignedHash64(final String seed) {
        Long res = 0L;
        res = FNV_64_INIT;
        int len = seed.length();
        for (int i = 0; i < len; i++) {
            res *= FNV_64_PRIME;
            res ^= seed.charAt(i);
        }
        if (res < 0) res = -res;
        return res;
    }

    public static Long getUnsignedHash32(final String seed) {
        Long res = 0L;
        res = FNV_32_INIT;
        int len = seed.length();
        for (int i = 0; i < len; i++) {
            res *= FNV_32_PRIME;
            res ^= seed.charAt(i);
        }
        if (res < 0) res = -res;
        return res;
    }

}
posted @ 2017-03-21 10:57  Andrew.Zhou  阅读(...)  评论(... 编辑 收藏