It might be better to cast all those values to longs, or use BigInteger, to avoid the risk of overflow.Seetharaman Venkatasamy wrote: . . . some technique to avoid the overflow . . .
Campbell Ritchie wrote:It might be better to cast all those values to longs, or use BigInteger, to avoid the risk of overflow.
Jesper de Jong wrote:Note: You probably meant
int result = (low+high) / 2;
Matthew Brown wrote:
Campbell Ritchie wrote:It might be better to cast all those values to longs, or use BigInteger, to avoid the risk of overflow.
in which case the conversion overhead would be something you'd want to avoid. It's probably once of those few cases where performance is really important. The Arrays.binarySearch() method does it that way.
Campbell Ritchie wrote: But are you going to have arrays bigger than 2^30 to search in the first place?
Seetharaman Venkatasamy wrote:I dont have an experience. but lets pretend
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Matthew Brown wrote:I think it comes down to the difference between >> and >>>. With the latter you get an overflow to a negative number, but you still end up with the right answer when you shift it because it adds a zero at the front.
The only thing you can get > 2147483647 elements into is a linked list. And you ain’t going to do a binary search on a linked list.Stephan van Hulst wrote:Pretend you have a 4GB array in memory? :P . . .
Campbell Ritchie wrote:Are the widths of pointers defined at all in C?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
"Il y a peu de choses qui me soient impossibles..."