3

How to iterate all possible values of bytearray of length = n in Python ? in worst case n <= 40bytes

For example, iterate for n = 4 :

00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000010

.
.
.
.

11111111 11111111 11111111 11111110
11111111 11111111 11111111 11111111
6
  • 1
    Worst case n is probably 8; 2^64 nanoseconds = 585 years Commented Apr 29, 2013 at 4:48
  • 1
    @JasonS: It's constant time! lol Commented Apr 29, 2013 at 4:49
  • I will not need all combinations :) i just need hint how to implement that - real usage will be a little bit different. Commented Apr 29, 2013 at 4:52
  • This is better done one by one mathematically. 2^320 for all combinations is a lot of combinations. Years of time and all the memory of all computers etc... 2^320 is 2135987035920910082395021706169552114602704522356652769947041607822219725780640550022962086936576 numbers... Commented Apr 29, 2013 at 5:18
  • Yes @MartinV. How do you ever expect to reach the worst case of 2135987035920910082395021706169552114602704522356652769947041607822219725780640550022962086936576? Commented Apr 29, 2013 at 6:29

2 Answers 2

4

You can use itertools.product:

In [11]: from itertools import product

In [15]: for x in product('01',repeat=4): #for your n=4 change repeat to 32 
    print "".join(x)
   ....:     
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Sign up to request clarification or add additional context in comments.

1 Comment

Thats great thank you. I am just reading http://docs.python.org/2/library/itertools.html#itertools.product and its really cool.
2

Inspired by https://stackoverflow.com/a/15538456/1219006

n = 2
[[[i>>k&1 for k in range(j, j-8, -1)] for j in range(8*n-1, 0, -8)] 
 for i in range(2**(8*n))]

You'll need to run this on Python 3 for large n cause xrange doesn't support big ints.

As a generator:

def byte_array(n):
    for i in range(2**(8*n)):
        yield [[i>>k&1 for k in range(j, j-8, -1)] for j in range(8*n-1, 0, -8)]

>>> i = byte_array(4)
>>> next(i)
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]
>>> next(i)
[[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1]]

Or if you don't want them grouped it's simpler:

[[i>>j&1 for j in range(8*n-1, -1, -1)] for i in range(2**(8*n))]

Equivalent generator:

def byte_array(n):
    for i in range(2**(8*n)):
        yield [i>>j&1 for j in range(8*n-1, -1, -1)]

1 Comment

Great job, a can learn a lot from your code. I am working on one crypto challenge and in few tasks i need to iterate very narrow intervals of bytearrays (grouped) so i was wondering about more general case - "how to iterate all combinations...". I know it would take almost ethernity to iterate one by one :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.