Common implementation methods and drawbacks

Two bit operation implementation methods and differences

Digression-find the average of multiple numbers

How to find the average of two integers?

The problem seems simple, but there are many things worth studying if it is implemented in code.

Below we use Java code to achieve.

## 1 Ordinary realization 1

```
private static int mean(int x, int y) {
return (x + y)/2;
}
```

```
Assert.assertEquals(Integer.MAX_VALUE,
mean(Integer.MAX_VALUE, Integer.MAX_VALUE));
```

```
java.lang.AssertionError:
Expected :2147483647
Actual :-1
```

## 2 Ordinary realization 2

```
private static int mean1(int x, int y) {
return (x + y) >>> 1;
}
```

`Assert.assertEquals(-6, mean1(-9, -34)); `

```
java.lang.AssertionError:
Expected :-6
Actual :2147483642
```

## 3 Ordinary realization 3

```
private static int mean3(int x, int y) {
return (x >> 1) + (y >> 1);
}
```

```
java.lang.AssertionError:
Expected :2147483647
Actual :2147483646
```

## 4-bit operation realization 1

```
/**
* IntMath#mean
* @link https://github.com/google/guava/blob/master/guava/src/com/google/common/math/IntMath.java
*/
//
private static int meanRoundDown(int x, int y) {
return (x & y) + ((x ^ y) >> 1);
}
```

We can understand the above code through collections.

For example, we find the average of the integers 9 and 3.

The binary of 9 is 1001, the binary of 3 is 0011,

Then the intersection of these two numbers is 9&3=0001,

The difference is 1010. 0001+(1010>>1) and the result is 6.

We all know that binary digits are a string of 0s and 1, so the integers x and y can be regarded as a set of many different 0s and 1s.

Then x & y means the intersection of two sets, because 1&1=1;

x ^ y gets the difference of the two sets, because 1^1=0,1^0=1;

The intersection and half of the difference is the binary average of the two numbers.

## 5-bit operation implementation 2

```
//
private static int meanRoundUp(int x, int y) {
return (x | y) - ((x ^ y) >> 1);
}
```

## 6 The difference between the two methods

```
Assert.assertEquals(6, meanRoundDown(9, 4)); //6
Assert.assertEquals(7, meanRoundUp(9, 4)); //7
```

## X digression

```
/**
* Stats#meanOf
* @link https://github.com/google/guava/blob/master/guava/src/com/google/common/math/Stats.java
*
* Returns the <a href="http://en.wikipedia.org/wiki/Arithmetic_mean">arithmetic mean</a> of the
* values. The count must be non-zero.
*
* <p>The definition of the mean is the same as {@link Stats#mean}.
*
* @param values a series of values
* @throws IllegalArgumentException if the dataset is empty
*/
public static double meanOf(int... values) {
checkArgument(values.length > 0);
double mean = values[0];
for (int index = 1; index < values.length; index++) {
double value = values[index];
if (isFinite(value) && isFinite(mean)) {
//Art of Computer Programming vol. 2, Knuth, 4.2.2, (15)
mean += (value - mean)/(index + 1);
} else {
mean = calculateNewMeanNonFinite(mean, value);
}
}
return mean;
}
```

End to sprinkle flowers.