The original binary search algorithm in the JDK used 32-bit integers and had an overflow bug if (low + high) > INT_MAX (http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html).If we rewrote the same binary search algorithm using (signed) 64-bit integers, can we assume that low + high will never exceed INT64_MAX because it's physically impossible to have 10^18 bytes of memory?When using (signed) 64-bit integers to represent physical quantities, is it reasonable to assume underflow and overflow can't happen?
Is it reasonable to assume that any physical quantity can be represented by a 64-bit integer without overflow or underflow?
design;algorithms
The short answer is no.  However, for some applications your assumption might be correct.Assuming a signed int, 2^63, with commas added for some clarity, = 9,223,372,036,854,775,808.  So it's roughly 9 * 10^18.  10^18 is an Exa.Wikipedia says As of 2013, the World Wide Web is estimated to have reached 4 zettabytes.[12], which is 4000 Exabytes.  Therefore, the WWW is roughly 400 times larger than 2^63 bytes.Therefore, there is at least one physical quantity that is much larger than a signed (or unsigned) 64 bit integer.  Assuming that your units are bytes.  If your units were something much larger, like GigaBytes, then you'd be o.k., but your precision of measurement would be low.For another example, consider far away galaxies.  The Andromeda Galaxy is actually one of the close ones, and it is 2.5 * 10^6 light years away.  If your units were miles, that would be 14.5 * 10^18, more than a 64 bit signed integer.  Now, obviously it depends on the units you use for your measurements, but some galaxies are way further away than Andromeda. (The furthest known one is 13 * 10^9 L.Y. away.)  Depending on the precision you want for your measurement, it could overflow a 64 bit integer.(Added)  Yes, miles are a lousy unit for astronomical distance.  A more normal unit might be an Astronomical Unit, roughly 93 million miles.  Using that unit of measurement, the furthest known galaxy is roughly 10^15 A.U. (if my math is right), which would fit into a 64 bit int.  However, if you wanted to also measure the distance to the Moon, to nearby orbiting satellites, that unit is too large.One more example from electronics: the Farad (F), a unit of capacitance.  Large capacitors range up to 5kF.  And this number will likely increase over time as Hybrid cars, smart grids, etc. improve.  Once can measure capacitance as small as 10^-18 F.  So the overall range in real capacitance that we can measure today is 5*10^21, larger than a 64 bit integer. 