Calculating mid in binary search
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The midpoint in binary search looks trivial, but the naive formula can overflow in fixed-width integer languages. That is why the standard implementation uses low + (high - low) / 2 instead of (low + high) / 2.
The Safe Midpoint Formula
Unsafe version:
Safe version:
The safe form avoids summing two potentially large integers directly.
Why Overflow Is the Real Issue
If low and high are both large, the expression low + high can overflow before division happens. That can produce:
- a negative number
- an invalid index
- a subtle search bug that only appears on large inputs
The midpoint itself may be representable, but the intermediate addition may not be.
Full Binary Search Example
This keeps midpoint calculation safe and the algorithm O(log n).
Integer Division Is Fine
Binary search does not require an exact fractional midpoint. Integer division rounds down, and that is completely acceptable because the algorithm only needs any valid index inside the current search interval.
For example:
Index 6 is a perfectly valid midpoint choice.
Inclusive Versus Exclusive Bounds
The midpoint formula works with either bound convention, but the rest of the loop must match the same convention.
Inclusive bounds example:
- '
low <= high' - '
high = mid - 1' - '
low = mid + 1'
Exclusive-upper-bound variants use different loop and update rules. Midpoint calculation is only one part of binary-search correctness.
The Same Idea Applies Beyond Arrays
This is not only about array indexes. Any binary search over a numeric range should compute the midpoint in a way that avoids unsafe intermediate addition.
For example, searching over answer space:
The same principle applies whether you are searching in an array, over timestamps, or over a numeric decision boundary.
Using Wider Integer Types
Sometimes developers switch index variables from int to long for very large search spaces. That can help, but the safe midpoint pattern is still the same:
Changing the type does not remove the habit of using the overflow-safe formula.
Common Pitfalls
Using (low + high) / 2 in languages with fixed-width integers is the classic overflow bug.
Assuming small tests prove the midpoint formula is safe can hide failures that only appear on large ranges.
Mixing inclusive and exclusive bound conventions often breaks binary search even when the midpoint formula itself is correct.
Focusing only on mid while ignoring the correctness of the bound updates creates search loops that never terminate or skip valid answers.
Summary
- The safe midpoint formula is
low + (high - low) / 2. - It avoids overflow that can happen with
(low + high) / 2. - Integer division rounding is expected and correct for binary search.
- Midpoint safety matters in both array search and numeric-range search.
- Correct binary search needs both safe midpoint calculation and consistent bound conventions.
Related reading
- Calculating Percentiles on the fly
- Calculating powa,b mod n
- Calculating SHA1 hash algorithm in PowerShell V2.0
- Calculating the nth digit of pi using the Bailey–Borwein–Plouffe BBP formula
- calculating the number of “inversions” in a permutation
- Calculating the shortest route between two points
- Calculating the similarity of two lists
- Calculating which tiles are lit in a tile-based game raytracing

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.