djb2 Hash Function

落花浮王杯 提交于 2019-11-29 04:35:16

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?

You shouldn't do that. Since there is no modulo, integer overflow is the expected behavior for the function (and it was designed with it in mind). Why do you want to change it?

I'm thinking your using a static/runtime analyser to warn about integer overflows? Well this is one of those cases where you can ignore the warning. Hash functions are designed for specific types of properties, so don't worry about the warnings from your analyser. Just don't try to create a hash function yourself!

return (hash & 0xFFFFFFFF); // or whatever mask you want, doesn't matter as long as you keep it consistent.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!