If I use bit shifting like this:
mid = low + (high - low) >> 1;
I'd get a "Time Limit Exceeded" message, but it's fine just using division:
mid = low + (high - low) / 2;
Wondering what's the problem here?
The issue is operator precendence:
- Division is done before addition
- Shifting is done after addition
low + (high - low) >> 1 is equivalent to
(low + (high - low)) >> 1.
mid = low + ((high - low) >> 1) should fix it.
@LinusVanElswijk Oh, I see. Thanks very much for your answer!