Fix subtle bug in binary search

Fix a bug in binary search code that could cause overflow with finite types like Int.