4 ms·
The latter two suggested fixes are also incorrect for arrays of length > INT_MAX where the goal element is close to the end, there won't be any out of bounds ar
by dtdt 11y ago
The latter two suggested fixes are also incorrect for arrays of length > INT_MAX where the goal element is close to the end, there won't be any out of bounds array access though.
For example, when low is 0x70000002, high is 0x90000000
int mid = (low + high) >>> 1;
and
mid = ((unsigned int)low + (unsigned int)high)) >> 1;
will both have mid as 1 due to unsigned int overflow.
- hofstee 11y agoThe article assumes the use of a signed integer, by passing in values that can't be represented with a signed integer you can't assume the same solutions hold.