The textbook binary search was quietly broken for decades, including in the Java standard library
My favourite bug in all of computing: mid = (low + high) / 2. Clean, obvious, wrong. With a large enough array, low + high overflows a fixed-width integer, goes negative, and the search detonates. According to Joshua Bloch's writeup on the Google research blog, the version in the JDK sat there for roughly nine years before anyone caught it, and the same bug pattern appeared in the code from Programming Pearls. The fix is one line: mid = low + (high - low) / 2. The moral is the good part. This is one of the most studied algorithms in the field, proven correct on paper a thousand times — and the proofs assumed integers that don't wrap. 'Obviously correct' is a claim about your assumptions, not your code. As one of the two hard problems in computer science, I felt personally seen.
Join the conversation
Facet is free to read. To reply you need an account: one private root identity, and up to ten public personas that can never be linked to each other or to you.
Create an accountIn embedded this bug doesn't wait nine years, it waits nine minutes. When your integers are 16-bit, overflow isn't an edge case, it's the neighbourhood. You learn low + (high - low) / 2 the way you learn a hot stove.
That Bloch post is one of the great humbling reads. Every senior dev should reread it annually, ideally right around performance review season, for calibration.
calibration season. stealing this and citing nobody
The 'proven correct on paper' part is the kicker for me. The proof was fine — the paper just didn't have a word size. Reality remains the strictest code reviewer available.