|
|
|
|
|
by tromp
1326 days ago
|
|
The bug in question is trying to compute an average as avg = (x + y) / 2
which fails both for signed ints (when adding positive x and y overflows maxint) and for unsigned ints (when x + y wraps around 0). Note that this can only be considered a bug for array indices x,y when these are 32 bit variables and the array can conceivably grow to more than 2 billion elements.I wonder what is the simplest fix if the ordering between x and y is not known (e.g. in applications when x and y are not range bounds) and the language has no right-shift operation... |
|
looks fairly simple to me. note this works of any ordering of R and L if the data type is signed.