binary-representation
integer-zeros
duplicate-article
number-theory
computer-science

Number of Zeros in the binary representation of an Integer

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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:

python
1def count_zeros_binary(n: int) -> int:
2    if n == 0:
3        return 1
4    bits = bin(n)[2:]
5    return bits.count("0")
6
7
8print(count_zeros_binary(9))   # 2
9print(count_zeros_binary(10))  # 2
10print(count_zeros_binary(15))  # 0

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:

python
1def count_zeros_bitwise(n: int) -> int:
2    if n == 0:
3        return 1
4
5    zeros = 0
6    while n > 0:
7        if (n & 1) == 0:
8            zeros += 1
9        n >>= 1
10    return zeros
11
12
13print(count_zeros_bitwise(9))   # 2
14print(count_zeros_bitwise(20))  # 3

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.

python
1def count_zeros_fixed_width(n: int, width: int) -> int:
2    bits = format(n, f"0{width}b")
3    return bits.count("0")
4
5
6print(count_zeros_fixed_width(5, 8))   # 6
7print(count_zeros_fixed_width(5, 16))  # 14

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:

python
1def count_zeros_twos_complement(n: int, width: int) -> int:
2    mask = (1 << width) - 1
3    bits = format(n & mask, f"0{width}b")
4    return bits.count("0")
5
6
7print(count_zeros_twos_complement(-5, 8))   # 1
8print(count_zeros_twos_complement(-5, 16))  # 1

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.

Course illustration
Course illustration

All Rights Reserved.