This was the famous “overflow-bug” of Binary Search that went undetected for over 20 years !!!

Even Java’s Binary search in java.util.Arrays had the same bug until it was reported to Sun Microsystems and fixed in 2006.

So, how to fix the bug?

We have already seen:

int mid = low + ((high - low) / 2);

A faster way to do so (in this particular case) is:

int mid = (low + high) >>> 1;

where, >>> is the unsigned Right Shift Operator.

In C/CPP where >>> is unavailable, following can be used:

mid = ((unsigned int)low + (unsigned int)high)) >> 1;

The binary-search bug applies equally to merge-sort, and to other divide-and-conquer algorithms. So, it’d be wise to fix your code implementations, if you did not know about this.

Second, did we overlook something? What do most blogs miss? A pragmatic analysis.

Of all the fuss about the famous Binary Search overflow-bug on Google research blog and many blogs about it, one thing is overlooked:

“Are we allowed to allocate array of size 2³¹? Can such a problem occur in real life systems?”

Memory Allocation:

Observe that an integer array of of size 2³¹ will require 8 GiB of memory.

For Java:

Well, it turns out, you can create an array of size 2³¹- 1 (or large enough for overflow condition to occur).

For C++:

Of course you cannot allocate that much memory statically on stack (you will get runtime error, crashing your program due to exceeding default stack size, typically 1–8 MB) but you can allocate that much memory on heap. So, overflow-bug condition can still occur in C++.

Theoretically, What if array size is > 2³¹, say, 2³². Well in that case, you will need to use 64-bit integers and again the same condition will arrive at 2⁶³ and so on. In reality, arrays of such sizes are generally not preferred due to consequent challenge of fitting them in memory.

So, depending upon usage and the magnitude of systems these days, it is possible that such a case might arise. Hence, it is recommended to write your implementations of Divide and Conquer algorithms (like Binary Search) considering such conditions.

In a nutshell: