Google Research Blog: Nearly All Binary Searches and Mergesorts are Broken [2006]
googleresearch.blogspot.com
googleresearch.blogspot.com
Turns out that the maximum for a Java integer happens in the middle of the numerical representations of the values that I thought were valid license keys. "Whoops!"
(Happily, it turns out that all the keys actually assigned to customers were accepted by the broken version of the checker, too.)
int mid = low + ((high - low) / 2);
is going to overflow if high and low have different signs. The correct version that works for any high and low is: int mid = sign(high) != sign(low) ?
(high + low) / 2 : low + (high - low) / 2;Your point is valid, though, and worth bearing in mind in other circumstances.