Efective way to avoid integer overflow when multiplying?

Posted by Jonathan on Stack Overflow See other posts from Stack Overflow or by Jonathan
Published on 2010-05-04T19:02:25Z Indexed on 2010/05/04 19:08 UTC
Read the original article Hit count: 248

Filed under:
|
|

Hi, I'm working on a hash function which gets a string as input.

Right now I'm doing a loop and inside the hash (an int variable) is being multiplied by a value and then the ASCII code for the current character is added to the mix.

hash = hash * seed + string[i]

But sometimes, if the string is big enough there is an integer overflow there, what can I do to avoid it while maintaining the same hash structure? Maybe a bit operation included inside the loop?

© Stack Overflow or respective owner

Related posts about c

    Related posts about hash