Safe integer middle value formula
I am looking for an efficient formula working in Java which calculates the following expression: (low + high) / 2 which is used for binary search. So far, I have been using "low + (high - low) / 2" and "high - (high - low) / 2" to avoid overflow and underflows in some cases, but not both. Now I am looking for an efficient way to do this, which would for for any integer (assuming integers range from -MAX_INT - 1 to MAX_INT). UPDATE : Combining the answers from Jander and Peter G. and experimenting a while I got the following formulas for middle value element and its immediate neighbors: Lowest