Number of Zeros in the binary representation of an Integer
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
Counting zero bits sounds simple, but the exact answer depends on what you mean by "binary representation." If you mean the minimal binary form with no leading zeroes, the task is different from counting zero bits in a fixed-width 8-bit, 32-bit, or 64-bit machine representation.
Decide Which Representation You Mean
For a positive integer written in minimal binary form, the rule is straightforward. For example, 9 is 1001, so it contains two zeroes.
In Python, you can compute that directly:
This version treats 0 as having the binary representation "0", so the count is one.
A Bitwise Approach
If you want to avoid converting the number to a string, you can count bits manually. One simple approach is to walk through the binary digits with shifts:
This inspects the number one bit at a time. It is a good approach when the problem is really about bit manipulation rather than string formatting.
Fixed-Width Binary Is a Different Problem
Sometimes the binary representation is defined by width, not by minimal length. For example, in 8 bits, 5 is 00000101, which contains six zeroes rather than one.
That distinction matters in systems programming, binary protocols, and interview questions. Always clarify whether leading zeroes count.
What About Negative Numbers
Negative values are where definitions start to diverge. In Python, bin(-5) returns -0b101, which is a signed string representation, not a fixed-width two's-complement machine value.
If you need a fixed-width signed representation, mask first:
Without that fixed width, "how many zeroes are in the binary form of a negative integer" is not a well-defined question in many languages.
Choose the Right Interpretation
If the problem comes from an interview or coding challenge, the expected interpretation is usually minimal binary for non-negative integers unless the prompt says otherwise.
If the problem comes from systems code, networking, or binary formats, fixed width is often the real requirement.
These are different tasks, and mixing them leads to off-by-many answers.
Common Pitfalls
The biggest mistake is forgetting to define whether leading zeroes count. Minimal binary and fixed-width binary give different results for the same number.
Another common issue is mishandling zero. In minimal form, 0 is usually represented as "0", so the zero count is one, not zero.
Negative numbers are another trap. If the question expects two's complement, you must know the bit width before counting zero bits meaningfully.
Summary
- Count zeroes differently depending on whether the binary form is minimal or fixed-width.
- '
bin(n)[2:].count('0')is the simplest approach for non-negative integers in minimal form.' - A bitwise loop is a good alternative when the task is about binary manipulation itself.
- Fixed-width representations require formatting or masking first.
- Clarify how zero and negative numbers should be treated before implementing the solution.
Related reading
- Occlusion algorithms collection
- On algorithm to find the median of n² implicit numbers
- Online Bayesian Learning in PyMCUpdating Posterior Beliefs Repeatedly
- Optimal algorithm to calculate the result of a continued fraction
- Optimal weights subset sum using backtracking
- Optimising the drawing of overlapping rectangles
- Optimized argmin an effective way to find an item minimizing a function
- Optimized low-accuracy approximation to rootnx, n

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.