Here you can find the source of meanSml(final int a, final int b)
public static int meanSml(final int a, final int b)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from w ww . j av a 2 s . c o m*/ * @return Mean without overflow, rounded to the value of smallest magnitude (i.e. mathematical (a+b)/2, using * integer division). */ public static int meanSml(final int a, final int b) { int result = meanLow(a, b); if (!haveSameEvenness(a, b)) { // inexact if ((a & b) < 0 || (a | b) < 0 && a + b < 0) { // both < 0, or only one is < 0 and it has the largest magnitude result++; } } return result; } /** * @return Mean without overflow, rounded to the value of smallest magnitude (i.e. mathematical (a+b)/2, using * integer division). */ public static long meanSml(final long a, final long b) { long result = meanLow(a, b); if (!haveSameEvenness(a, b)) { // inexact if ((a & b) < 0 || (a | b) < 0 && a + b < 0) { // both < 0, or only one is < 0 and it has the largest magnitude result++; } } return result; } /** * @return Mean without overflow, rounded to the lowest value (i.e. mathematical floor((a+b)/2), using floating * point division). */ public static int meanLow(final int a, final int b) { return (a & b) + ((a ^ b) >> 1); } /** * @return Mean without overflow, rounded to the lowest value (i.e. mathematical floor((a+b)/2), using floating * point division). */ public static long meanLow(final long a, final long b) { return (a & b) + ((a ^ b) >> 1); } /** * @return True if the specified values are both even or both odd, false otherwise. */ public static boolean haveSameEvenness(final int a, final int b) { return ((a ^ b) & 1) == 0; } /** * @return True if the specified values are both even or both odd, false otherwise. */ public static boolean haveSameEvenness(final long a, final long b) { // faster to work on ints return haveSameEvenness((int) a, (int) b); } }