The Bijection Between Natural Numbers and Binary Strings
Computers fundamentally operate on bits and sequences of bits. It is therefore useful to study the associated mathematical structure of the concept of sequences of bits. In this blogpost we give a brief introduction to the mathematical concept of finite binary strings and it's relation to the natural numbers.
We write a binary string of length n as where .
We define the concatenation of two binary string and as .
Let the set of all finite binary strings with the empty string. We call the length of a binary string, i.e. .
Let be the function that takes a number and returns its binary representation, i.e. .
by itself won't give us a bijection because we can't match binary strings like or .
To get a bijection we have to use the following function from to
This function simply adds one to the number , than represents as a binary and takes the substring from the second character on. The first character is always a one and we drop it.
For example we have or where we use the convention that a slice with is empty.
We now proof that the mapping above is indeed bijective:
Injective: Suppose , this would mean . This can only be the case when . But than equality would mean that because both have the same leading one as first char. That implies because the binary representation of a number is unique. Therefore we can conclude that is injective.
Surjective: Let be an arbitrary binary string. Concatenate this string with from the left side and call this string . The natural number which fulfils is the number that fulfils .
More formally we may define the inverse as
is the natural mapping that interprets the binary string as a natural number.
Example:
,
.
Note that because the min value is obtained iff all are
Note that because the maximum value is obtained iff all are
We have and therefore . Furthermore we have .
This gives us an effective bound for :
This shows that the range in which a number associated with a binary string is determined solely by the length of the binary string.
Note that the above bijection is often used in literature such that we write for and instead of if clear from the context. It also gives us a way to enumerate all binary strings.