Your last approach seems promising. You can improve on that by manually considering the lowest bits of a and b:
unsigned int average = (a / 2) + (b / 2) + (a & b & 1);
This gives the correct results in case both a and b are odd.
Related Contents:
- Integer division always zero [duplicate]
- How approximation search works
- Is there a standard sign function (signum, sgn) in C/C++?
- C/C++: Pointer Arithmetic
- Can’t make value propagate through carry
- Fastest way to determine if an integer is between two integers (inclusive) with known sets of values
- How can I write a power function myself?
- How does photoshop blend two images together? [closed]
- Creating all possible k combinations of n items in C++
- Finding all the subsets of a set
- Representing 128-bit numbers in C++
- Increasing accuracy of solution of transcendental equation
- Fast ceiling of an integer division in C / C++
- How to achieve smooth tangent space normals?
- Most accurate way to do a combined multiply-and-divide operation in 64-bit?
- Why does long long n = 2000*2000*2000*2000; overflow?
- What is the best way to evaluate mathematical expressions in C++?
- Why isn’t `int pow(int base, int exponent)` in the standard C++ libraries?
- What are the most widely used C++ vector/matrix math/linear algebra libraries, and their cost and benefit tradeoffs? [closed]
- Confusion between C++ and OpenGL matrix order (row-major vs column-major)
- Why is size_t unsigned?
- Calculating (a^b)%MOD
- A warning – comparison between signed and unsigned integer expressions
- How to convert Euler angles to directional vector?
- Direct way of computing clockwise angle between 2 vectors
- Overflowing of Unsigned Int
- Most efficient/elegant way to clip a number?
- Direct way of computing the clockwise angle between two vectors
- Uses of a C++ Arithmetic Promotion Header
- What is going on with bitwise operators and integer promotion?
- Does casting to an int after std::floor guarantee the right result?
- Area of rectangle-rectangle intersection
- What is half open range and off the end value
- Rotate and translate object in local and global orientation using glm
- Compute objects moving with arrows and mouse
- What is the C++ function to raise a number to a power?
- Fastest implementation of sine, cosine and square root in C++ (doesn’t need to be much accurate)
- How computer does floating point arithmetic?
- How do I implement a Bézier curve in C++?
- Best library for statistics in C++? [closed]
- Why can I use auto on a private type?
- Is (4 > y > 1) a valid statement in C++? How do you evaluate it if so?
- “undefined reference to” errors when linking static C library with C++ code
- Returning a reference to a local or temporary variable [duplicate]
- error: passing xxx as ‘this’ argument of xxx discards qualifiers
- Replacement for deprecated register keyword C++ 11
- When is an object “out of scope”?
- Casting pointer to Array (int* to int[2])
- pthreads: thread starvation caused by quick re-locking
- How can I make the map::find operation case insensitive?