That said, it's always safer to compute the mean of two integers as
min + (max - min) / 2
to avoid integer overflow.
That said, it's always safer to compute the mean of two integers as
min + (max - min) / 2
to avoid integer overflow.
#include <iostream>
using namespace std;
int avg(int a, int b) {
return (a&b) + ((a^b)>>1);
}
int main() {
cout << avg(int(2e9), int(2e9 + 10)) << endl;
cout << avg(int(2e9), int(-2e9)) << endl;
cout << avg(int(-2e9), int(-2e9 - 10)) << endl;
return 0;
}
Gives: 2000000005
0
-2000000005 int mid = (low + high) >>> 1;
This was a fix because the original code had a bug: int mid =(low + high) / 2;
Which was part of the Java code base for many years. Joshua Bloch based the code on Programming Pearls, which also contained the bug.More info: https://research.googleblog.com/2006/06/extra-extra-read-all...
It's actually really simple. We'll write a and b in binary notation, for example:
a = 1001101
b = 0100111
Now what happens when you add two numbers in binary? We essentially add the numbers in each column together, and if it overflows, we carry to the next column (this is how you carry out addition in general).So what are the columns where we need to carry, the ones that overflow? These are given by (a&b) - the columns where both a and b contain a one. To actually carry we just move everything one position to the left: ((a&b)<<1). And what are the columns where we don't need to carry, the ones that don't overflow? These are the ones where we have exactly one zero, either in a or in b, so: a^b.
In other words, a + b = ((a&b)<<1) + (a^b). To compute the average, we divide by two, or in other words, we bitshift to the right by one place: (a + b)/2 = (a&b) + ((a^b)>>1)
If anything is unclear, feel free to ask :)
[1] http://www.inwap.com/pdp10/hbaker/hakmem/boolean.html#item23