I need to sample integers from an ordered array described as follows.
Let k
be a positive integer.
All entries are nonnegative integers in
[0,2^k)
The list starts at
0
- All the (increasing) integers with hamming weight 1 modulo bit shifting (i.e. multiplication by 2) follow.
- All the (increasing) integers with hamming weight 2 modulo bit shifting, follow, and so on.
The array for k=5
looks like this:
0 ( weight 0 )
1 ( weight 1 )
11 ( weight 2 )
101
1001
10001
111 ( weight 3 )
1011
1101
10011
10101
11001
In particular, given an entry on the list, I would like to deduce the next entry algorithmically.
I know this can be done in several ways (see e.g. this question). For the sake completeness, this is how these other arrays looks like, in contrast to the one above:
0 ( weight 0 )
1 ( weight 1 )
10
100
1000
10000
11 ( weight 2 )
101
110
... etc