Tagged articles
2 articles
Page 1 of 1
Java Architect Essentials
Java Architect Essentials
May 21, 2022 · Fundamentals

How to Compute an Unsigned Integer Average Without Overflow

This article explores why the naïve (a + b) / 2 formula overflows for 32‑bit unsigned integers and presents several safe techniques—including subtraction‑first, bitwise‑based, wide‑type casting, and carry‑rotate instructions—complete with C++ code and assembly examples.

AssemblyCaverage
0 likes · 11 min read
How to Compute an Unsigned Integer Average Without Overflow