Reputation: 2818
In what way(s) would be an overflow in an expression be desirable in the following cases:
?
Upvotes: 0
Views: 124
Reputation: 1503859
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:
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.
Upvotes: 4