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.
More Related Contents:
- how can i increase the speed of my C++ calculation [closed]
- C++ quadratic equation using factorization
- Why does my code work? Simple arithmetics
- Integer division always zero [duplicate]
- Can’t make value propagate through carry
- How does photoshop blend two images together? [closed]
- Fast ceiling of an integer division in C / C++
- Direct way of computing clockwise angle between 2 vectors
- Why is size_t unsigned?
- A warning – comparison between signed and unsigned integer expressions
- Most accurate way to do a combined multiply-and-divide operation in 64-bit?
- What is the best way to evaluate mathematical expressions in C++?
- Uses of a C++ Arithmetic Promotion Header
- Most efficient/elegant way to clip a number?
- Math to convert seconds since 1970 into date and vice versa
- Does casting to an int after std::floor guarantee the right result?
- Compute objects moving with arrows and mouse
- How do I implement a Bézier curve in C++?
- Random number generator that produces a power-law distribution?
- Special simple random number generator
- sine wave that slowly ramps up frequency from f1 to f2 for a given time
- Impact of cubic and catmull splines on image
- Convert string to mathematical evaluation [closed]
- Inverting a 4×4 matrix
- OpenSouce C/C++ Math expression parser Library [closed]
- Direct way of computing the clockwise angle between two vectors
- Fast divisibility tests (by 2,3,4,5,.., 16)?
- Fastest way to get the integer part of sqrt(n)?
- Finding square root without using sqrt function?
- Is there any advantage to using pow(x,2) instead of x*x, with x double?