Why is the enumeration of binary strings misaligned?

466 Views Asked by At

If you enumerate the set of binary strings in ascending order, you get:

0 = 0
1 = 1
2 = 00
3 = 01
4 = 10
5 = 11
6 = 000
7 = 001
8 = 010
9 = 011
10 = 100
11 = 101
12 = 110
13 = 111
14 = 1000
...

Notice that this is kinda misaligned. If you start counting from 2, then each string that increases the length is at a 2^N index:

2 = 0
3 = 1
4 = 00
5 = 01
6 = 10
7 = 11
8 = 000
9 = 001
10 = 010
11 = 011
12 = 100
13 = 101
14 = 110
15 = 111
16 = 1000
...

This seems much more natural. For example, now there is a simple formula to map a natural number to the corresponding binary string:

bin(0) = bin(1) = []
bin(x) = b : bin(x - a/(2-b))
    where a = 2 ^ floor(log(2,x))
          b = (2/3) * x/l

Is there any reason for that misalignment? Is there a name for this enumeration/formula?

1

There are 1 best solutions below

1
On

To enumerate binary strings, start with the empty string.
To count, start with $1$.

1 =    
2 = 0
3 = 1
4 = 00
5 = 01
6 = 10
7 = 11
8 = 000
9 = 001
10 = 010
11 = 011
12 = 100
13 = 101
14 = 110
15 = 111
16 = 1000
...

Presto! Not mis-aligned.

another answer
The geometric series $$ 1+2+2^2+2^3+\dots +2^n = 2^{n+1}-1 $$ is already mis-aligned by $1$; and if you leave out the empty string $$ 2+2^2+2^3+\dots +2^n = 2^{n+1}-2 $$ it's mis-aligned by $2$.