djb2 哈希函数 [英] djb2 Hash Function

查看:13
本文介绍了djb2 哈希函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 djb2 算法生成如下字符串的哈希键

I am using the djb2 algorithm to generate the hash key for a string which is as follows

hash(unsigned char *str)
{
    unsigned long hash = 5381;
    int c;

    while (c = *str++)
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

    return hash;
}

现在每个循环都有两个大数字的乘法,一段时间后字符串的第 5 个字符的第 4 个字符会溢出,因为哈希值变得很大

Now with every loop there is a multiplication with two big numbers, After some time with the 4th of 5th character of the string there is a overflow as the hash value becomes huge

重构的正确方法是什么,以便哈希值不会溢出并且哈希也正确发生

What is the correct way to refactor so that the hash value does not overflow and the hashing also happens correctly

推荐答案

哈希计算经常溢出.这通常根本不是问题,只要您保证当它确实溢出时会发生什么.不要忘记,散列的重点不是有一个数字,这意味着在大小等方面的意义 - 这只是检测相等性的一种方式.为什么溢出会干扰?

Hash calculations often overflow. That's generally not a problem at all, so long as you have guarantees about what's going to happen when it does overflow. Don't forget that the point of a hash isn't to have a number which means something in terms of magniture etc - it's just a way of detecting equality. Why would overflow interfere with that?

这篇关于djb2 哈希函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆