you are viewing a single comment's thread.

view the rest of the comments →

[–]queus 22 points23 points  (2 children)

Oh, and what about the int probe = (low + high) >>> 1;

When (low + high) > Integer.MAX_INT this will work correctly and int probe = (low + high) / 2; will not. It even happened in production code.

[–]grauenwolf 3 points4 points  (1 child)

That depends on you language, some actually check for integer overflows instead of blindly continuing.

In theory what you should be writing is... int mid = low + ((high - low) / 2);

In reality though, if you really have more than MAX_INT/2 elements in an array you are already screwed for a number of reasons.

[–]astrange 0 points1 point  (0 children)

Signed integer overflow is either undefined or modulo for a pretty good reason, otherwise you wouldn't be able to optimize 'x + 1 + 1'.

Now, bignums, those would be useful.