So in this week's bonus video,
I wanna give a quick overview or crash course
into how to work with binary numbers.
I think everyone has some basic intuition
that computers use binary numbers
to represent any form of data.
And so, by binary,
what we mean is just a series of zeros and ones, right?
And by binary numbers,
what we'll mean is a series of zeros and ones
that represent integers,
because that's what we're gonna be concerned
about this week.
So, our binary numbers could be 1001.
And that's not 1,001.
That's encoding something else.
And so, I use the phrase encoding.
You'll often hear people talking about encoding
or decoding binary numbers.
I forget which is which,
'cause really it depends, if you're a machine or a human,
which one do you think is the code,
which one do you think is the more natural representation.
So let's make it a little more obvious,
what we're talking about.
Let's go binary to integers, right?
So, someone gives you a number, like 1001.
You could think it's 1,001.
If you know they're talking in binary,
how do you figure out what it is that they're giving you?
What is 1001?
And so, the key intuition that you need
to have is that you wanna encode numbers
with only one digit.
You can either have 0 or 1.
And those could be any numbers,
but it makes sense to use them
to represent the two lowest numbers.
And so, we wanna represent 0 and 1 using 0 and 1.
So it makes sense to say that 0 is 0 and 1 is 1, right?
The ones on the left here are binary numbers.
They could be on and off.
They're just two symbols.
The one on the right are numbers, actual integers, okay.
Now, if I add another digit,
well, that gives me access to twice as many numbers, right?
We've done this game before.
How many combinations of zeros and ones do you have
in one cell?
You have 2.
One cell is either 0 or 1.
In two cells, 2 times 2.
In three cells, like in the elementary CA,
it's 2 times 2 times 2, 8.
And so, 2 times 2 would be 4.
So really, what I've encoded here is 2 and 3.
And I could go on and add another series of zero,
and then keep going.
100 would be be 4.
101, I'm just repeating the same sequence, as of there,
would be 5, and so on and so forth.
All the way to 111, which here would be 7, right?
It's 7 because I encode two numbers using one digit.
That's 0 and 1.
So that goes to like, because I start index at 0,
I go to the number of numbers minus one.
That's three little dots.
Okay, so that's the process of getting binary to numbers.
But what if someone gives you a binary sequence
and you wanna decode it?
Well, now that we know this process,
we can realize a little bit how it works.
That first digit on the left, the number of digit,
let's start with there,
is telling me the space of numbers that are encoded.
I know the minimum value is 0.
The maximum value will be 2 to the n-1, right?
That last digit, or the first digit on the left,
is gonna tell me whether you're in the top half,
if it's a 1, or the bottom half, if it's a 0.
The next digit will be top half of the half you're in,
or bottom half, and so on and so forth,
until the last digit,
which is essentially gonna tell me if you're odd or even
when you're down to two numbers, right?
If you look at it,
all the zeros are always gonna be evens
and all the ones are always gonna be odd.
That's the way we were writing it down.
And so, the realization you can have from the scheme is
that this is 2 to the 0.
It's gonna give you a plus 1.
2 to the 1, 2 to the 2, right?
And so, that's encoding this division and half scheme of,
are you in the top half, bottom half?
Are you in the top half, bottom half?
All the way down to, are you add or even?
And summing them up,
based on whether their 0 or 1 times 2 to the n
or n = 0 to the number of digits, minus 1,
is gonna give you your integer.
Okay, it's a little abstract.
So, how do we go from 1001 to an integer?
How do we go from binary to integer?
Well, that first one is telling me 2 to the 0.
And when I say first one, I'm starting here,
which is not intuitive.
So I have 1 times 2 to the 0 plus 0 times 2 to the 1,
plus 0, and I'm reading backwards, right, the digits.
So that was my 2 to the 1, 2 to the 2.
And then, that one
on the left is gonna give me a 1 times 2 to the 3.
That's telling me sort of like I'm in the top half,
but bottom half after that, bottom half after that,
and then I'm odd.
And so, what is this?
That's 1 + 0 + 0 + 8.
That's 9.
So that 1001 was simply 9 in binary representation.
Great.
So that's how you go from binary to integers.
And this last formula, actually,
is a really good starting point for the next step,
which I'm gonna switch color for,
which is gonna be integers to binary.
And here, I'm connecting this
to the content of this week a little bit more.
Because you know, binary to integers is useful,
because you can see like, on the left,
I represented, you know, 000 could be three,
my state and the state of my two neighbors in a CA, right?
We're all off, we stay off, or something like that.
Like that could be the rule.
And here, what we're saying is that 000 is rule number 0.
001 is rule number 1.
So that gives us an ordering scheme for the rules
in the rule set of elementary CA.
What integers to binary is gonna do is,
if I wanna ask my code, "Run rule 30,"
that we saw in the introduction video,
well, we need to be able to know rule 30 is what sequence
of one and zeros?
And so, the way that works is
through an algorithm called, whoops,
let's just get back to where we were,
an algorithm called divide-by-two algorithm.
All right, algorithm makes it sound fancy.
Divide-by-two makes it sound easy,
and it's a little bit of both.
And so, let's say we have any number, right?
That number can be represented as a sum of 1 times 2
to the 0 plus 0 times 2 to the 1 plus 0 times 2
to the 2 plus 1 times 2 to the 3.
This is the number 9 that we saw.
And we could go on 2 to the 4, 2 to the 5,
2 to the 6 for really big numbers.
So that's the sum over the powers of 2.
Well, what can we do with sums over the power of 2?
We can say, "Cool, that's like 1 plus 2 times 0 times 2
to the 0 plus 0 times 2 to the 1.
I just pulled out, a common factor of 2 in all the digits
but the very last one on the right, right?
What does that give us?
And I'm just gonna make space for us, you know?
Well, that means I can take my number 9.
I can take my number 9, and ask,
"Are you even or odd, right?"
And I'm operating at that level.
If you're even or odd,
I know that your last digit on the right is 0 or 1.
It's 1, if you're odd.
It's 0, if you're even.
So I can ask, "Are you even or odd?"
Take care of that bit.
Then, I can divide by 2
to get rid of this division by 2.
Take the remaining number
which is gonna be everything that's here
and divide that by 2.
And in reality, as we'll see in the code,
we're just gonna ask, "Are you even or odd?"
And we're gonna do a division by 2 followed by a 4, right?
So if this entire thing is n and n=9, we're gonna ask,
"Is n even or odd?"
Actually, let me do it explicitly.
Is 9 even or odd?
That's a 1 at the end.
Then, we're gonna divide by 2.
That's a 4.5, and floor it.
The reason for the floor is that we wanna get rid
of whatever the answer was and then divide it by 2.
So I just pointed out, and let me highlight it.
I'm just gonna highlight this digit here.
If this is 1,
it's gonna give me a .5 after the division by 2.
If it's 0, it's gonna give me a .0.
So if I floor, I get rid of whatever was in my yellow box.
So what we wanna do is divide by 2, then floor.
And we know that after a divide by two, then floor...
And by floor, I mean round down to the nearest integer.
I know that after I divide by two followed by floor,
what I'm left with is the number underlined in yellow, okay?
So in the case of my 9, that's a 4.
Even or odd?
Whoops.
I'm having a hard time spelling tonight.
Even or odd?
It's even, and so that means 0.
And then, we would keep going.
Is 2 even or odd?
It's a 0, right?
It's even.
Oh, my god.
So that was odd, even, even.
And then, we'd have 1.
Even or odd?
And that's a 1, 'cause it's odd, right?
So we just do the sequence of dividing by two and flooring,
and then that gives us, reading from top to bottom,
that gives us a 1001 for the number 9.
And so, really, we're just encoding any number
in the space of 2 to the n numbers.
Are you in the top half, bottom half,
top half, bottom half, top half?
And with a sequence of yes or no,
or in a sequence of even or odd remainder,
we can encode or decode any numbers into a binary sequence.
So that's the intuition behind it.
We're gonna use that, or you will use that,
depending on your watching order,
to make really flexible code that can encode any rule set
with a given neighborhood for elementary CA,
where the cells are the zeroes and ones.
And so, I'll see you in the next one
and in our discussion group.
And this is all the basics that we need to be able
to play with CA, and there's a lot to play with.
So, this should be a fun one.
Bye.
See you soon.