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:
- '
2choices for the first bit' - '
2choices 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.
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.
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.
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.
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:
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].
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
4means16outputs - all distinct rearrangements of
0011means only6outputs
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
xhas2^xpossible outputs. - Use recursion for clarity,
itertools.productfor conciseness, or bit operations for a numeric approach. - Prefer generators when
xis 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.

