0

In what way(s) would be an overflow in an expression be desirable in the following cases:

  1. Calculating a hash value
  2. Calculating a check sum

?

3
  • Could you be a little more verbose? I do not think I undestand the question... Commented Jan 11, 2009 at 16:02
  • Sounds like a question from a textbook - out of the context. But it seems that Jon already had an answer... :-) Commented Jan 11, 2009 at 16:18
  • This does sound like homework. Commented Jan 11, 2009 at 16:19

1 Answer 1

4

So long as you're expecting that operations may overflow, and that the definition of what you're trying to do allows for overflow (or possibly requires it as a result) it makes life a lot easier.

For instance, one algorithm for calculating a hash is:

  • Start with a prime number
  • For each field in your object, multiply the current result by another prime number, and add the hash of the field

For example:

int ret = 37;
ret = ret * 37 + field1.GetHashCode() * 23;
ret = ret * 37 + field2.GetHashCode() * 23;
ret = ret * 37 + field3.GetHashCode() * 23;
return ret;

Here, any of the calculations could overflow, but that's okay - we're not really interested in the magnitude of the number, just the aim that it's likely to be different for different objects, but the same for equal ones.

Trying to do this without overflow is a pain - you could use XOR, but that has other downsides.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.