I showed why binary has to be the way it is.. in a way
So I figured out this cool way which shows how and why binary numbers work. And more interestingly, why they are uniquely defined.
Obviously, we all know binary uses
as its place values.. with the goal of representing every number possible from 1 to sum(of numbers). Usually, this is just presented as the way binary works and is defined. But let's look at it in a different way: why these numbers? Could we have chosen some other sequence, to represent numbers in general?
Turns out, if we put a few natural constraints on the problem, we don't really have a choice.
Let's forget binary exists
Suppose I want to construct a number representation system where I have some set of basis values
and for every basis value I can choose a coefficient of either or .
So every number I represent looks like
where every .
In other words, I am allowed to take or not take each basis value.
What do I want from this system?
1. I want to represent every number
If the sum of all my basis values is , then the largest thing I can possibly make is .
So I want my system to be able to make every integer from to .
No gaps.
If I can make but not , that's not a very useful representation system.
2. I want the representation to be unique
I don't just want to be able to make every number. I want each number to have exactly one representation.
Otherwise, the same number could have multiple different combinations of basis values(i.e. it could have multiple representations), which rather defeats the point of having a clean representation system.
3. I want the system to be scalable
This one is slightly more subtle.
Suppose I have a valid system with basis values. If I remove the last few values, I still want the remaining prefix to be a valid system in itself.
So if
is valid, then
should also be valid for every .
This means that I don't want some weird system that only works at certain specially chosen lengths. I want to be able to keep extending the system forever while preserving the same rules.
Now let's see what these constraints actually force.
What can the next number possibly be?
Suppose I already have a valid system
and these values can represent every integer from
where
Now I want to add one more basis value, .
What can possibly be?
Let's enumerate what the new system can make.
First, without using , I can already make
That's guaranteed by our assumption.
Now let's use .
Every number I could previously make can now be added to , so I can make
So the set of numbers I can make is really just two intervals:
and
And now the whole thing becomes obvious.
These two intervals have to join together without either a gap or an overlap. What do I mean exactly?
Say, If
then cannot be represented. There is a gap.
For example, if I can make everything up to , and my next basis value is , then is impossible.
So we need
However!
If
then itself was already representable using the old basis values(by defintion! since we assumed that the system works till ). Now I have at least two representations of it:
- using the old values;
- using itself.
That's forbidden.
So we need
Put the two constraints together:
There is exactly one integer satisfying this.
And that's it.
There is no choice.
And now watch what happens
We need to be representable, so the smallest possible starting basis value is
The sum so far is , so the next value has to be
Now the sum is
so the next value has to be
The sum is now
so
And so on:
We have just been forced into
which is exactly the binary place-value sequence.
In fact, if
then
So the next value is always exactly twice the previous one.
So why binary?
The way I now think about it is that binary isn't really an arbitrary convention.
If you decide that:
- every basis value can be used either or times,
- every integer in the possible range must be representable,
- every representation must be unique, and
- the construction must remain valid as you keep extending it,
then the basis values are forced to be powers of two.
The reason is basically just:
Don't leave gaps. Don't create duplicates.
If your current system covers everything up to , the next value has exactly one place it can go:
And repeatedly applying that rule creates binary.
Pretty neat that something which feels like a definition is actually, under these constraints, a necessity.
One ofcourse, may object that these contraints have themselves come from our familiarity with binary.. that is something left to your discretion! I feel these contraints are pretty general and make sense for a self-explainable working number representation system.
And, amusingly, this whole rabbit hole started while I was solving the CSES Missing Coin Sum problem. XD