binary permutations
sequence generation
combinatorial mathematics
binary sequences
algorithm design

all permutations of a binary sequence x bits long

Master System Design with Codemia

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

Introduction

For a binary sequence of length x, there are 2^x distinct sequences because each position can be either 0 or 1. People often call these “permutations,” but in practice the problem is usually to enumerate all binary strings of a fixed length. The right solution depends on whether you want clarity, speed, or lazy generation.

Understand the Count First

If each bit position has two choices, the total number of length-x binary sequences is:

  • '2 choices for the first bit'
  • '2 choices for the second bit'
  • and so on

So the total is:

2^x

That means the output size itself grows exponentially. Even with a perfect algorithm, generating all sequences of length 30 already means more than one billion results, so the real constraint is usually output size rather than CPU trickiness.

Recursive Generation

A clean way to generate all binary sequences is to build them one bit at a time.

python
1def generate_binary_strings(n, prefix=""):
2    if n == 0:
3        yield prefix
4        return
5
6    yield from generate_binary_strings(n - 1, prefix + "0")
7    yield from generate_binary_strings(n - 1, prefix + "1")
8
9
10for bits in generate_binary_strings(3):
11    print(bits)

This is easy to understand because the recursion matches the structure of the problem: each shorter prefix branches into a 0 and a 1.

Use itertools.product

If you want a concise and idiomatic Python solution, itertools.product is often the best choice.

python
1from itertools import product
2
3for bits in product("01", repeat=3):
4    print("".join(bits))

product("01", repeat=n) gives the Cartesian product of the two symbols across n positions. It is usually the cleanest way to express the problem when you do not need to teach recursion specifically.

Generate with Bit Operations

If you want a more numerical view, iterate through integers from 0 to 2^x - 1 and format each number as a binary string.

python
1def binary_strings_via_numbers(n):
2    for value in range(1 << n):
3        yield format(value, f"0{n}b")
4
5
6for bits in binary_strings_via_numbers(4):
7    print(bits)

This approach is compact and efficient. It is also useful when the binary sequence directly corresponds to a bitmask or subset representation.

Return Lists Only When You Need Them

Because the output grows so quickly, generators are usually better than building a full list.

python
strings = list(generate_binary_strings(4))
print(strings)

That is fine for small n, but for larger values it is much safer to stream the results one at a time and process them incrementally.

For example:

python
1count = 0
2for bits in generate_binary_strings(20):
3    count += 1
4
5print(count)

Here, you avoid storing all one million-plus sequences in memory at once.

If You Need Lists of Integers Instead of Strings

Sometimes the real requirement is not textual output, but sequences such as [0, 1, 1, 0].

python
1from itertools import product
2
3for bits in product([0, 1], repeat=3):
4    print(list(bits))

Choosing the representation early matters because it affects later code. Strings are convenient for display, while integer tuples are often better for algorithmic processing.

Be Precise About Terminology

Strictly speaking, “all permutations of a binary sequence of length x” can be misleading because permutations usually mean rearrangements of an existing collection. If you already know the exact number of zeros and ones, that is a different problem from generating every possible binary string of length x.

For example:

  • all binary strings of length 4 means 16 outputs
  • all distinct rearrangements of 0011 means only 6 outputs

Those are related, but not the same task.

Common Pitfalls

The biggest mistake is underestimating how fast the output grows. The algorithm may be fine, but the result set becomes unmanageable quickly.

Another issue is confusing “all binary strings of length x” with “all rearrangements of one given binary sequence.”

People also build huge lists in memory when a generator would let them process the results lazily.

Summary

  • A binary sequence of length x has 2^x possible outputs.
  • Use recursion for clarity, itertools.product for conciseness, or bit operations for a numeric approach.
  • Prefer generators when x is large so you do not store every result at once.
  • Pick the output representation that matches the next step of the algorithm.
  • Be precise about whether you mean all binary strings or permutations of one fixed sequence.

Course illustration
Course illustration

All Rights Reserved.