0:11

So we have a number and we want to write it to the ee prompt.

And it's a two byte number.

So it's an integer, it's got two bytes.

And in this case we're using the number 259.

So 259 in binary is shown there at the top.

A sequence of zeros and ones.

And notice it has three ones in it.

Okay. It's got 16 bit positions total.

But in binary, three of those positions are ones and the rest are zeros.

The least significant two bits are ones, and

the bit in the eighth position is a one.

And the rest of them are zeroes.

So that's 259.

And we wanna put that into EEPROM.

So first, we wanna just focus on the least significant byte.

Then we wanna focus on the most significant byte.

So the least significant byte are the least significant eight bits, and

they include two of those 1s, okay.

The low two, the least significant 1s are in there.

Then the most significant byte has that other 1 in it.

1:00

So we wanna focus on the least significant byte first, so how do we do that?

What we do, is we create a mask.

Now the mask you can see, is valued 255.

That's its decimal representation.

But in binary, you see it right there underneath the 255.

It's, basically it looks like it's high bits, high eight bits or all zeros.

And its low eight bits which are highlighted red are all ones.

So what this is trying to tell us is, in our mask,

is that the bits that we are interested in our original number 259.

Are the low bits, the least significant eight bits.

Those are the ones that the mask has assigned to one.

So, those are gonna be the bits, so at least look at 8 bits inside 259,

those are the ones that we're interested in right now.

1:53

So we do a bit wise and.

Now bit wise and, so an and operation is basically a logical operation,

it takes 0s and 1s or highs and lows.

And an and operation only outputs a 1 if both the inputs are 1.

So if you and 0 and 0 that's a 0, 0 and 1 is a 0, 1 and 0 is a 0, 1 and 1 is a 1.

So that's an and.

So bitwise and, what that does is it performs

ands like I just said on every pair of bits in the two numbers.

So if we look at those two numbers, 259 and 255,

look at their bit representations, they're 16-bit numbers.

So what actually happens when you do a bitwise and

is it performs 16 different and operations.

So it takes the least significant bits, the bits in the zeroth position,

ands those, and then it gets the zeroth position of the result.

Which in our case, if you look at the mask, the zeroth bit is a 1,

and the zeroth bit, the least significant bit of 259 is also 1.

You add 1 and 1, you get a 1.

So the result is a 1, right?

In the bottom row, that result you see is a 1, in that least significant bit.

You can say the same thing for bit position one.

Yeah, there's a one in 259 in that bit position and

there's a one in the mask in that position.

So you get a one in the result and the position of one.

And when you go to position number two, you get a zero in 259 right?

So whenever you end something with a zero, you get a zero.

So in that position in the result, you see a zero and so on.

3:17

So the key here is that when you and something,

in this case, we're taking the least significant eight bits of 259 and

anding them with 1s, because the mask is all 1s.

When you take a number, bitwise and it with 1s, you get that same number back.

So you'll notice in the results at the bottom,

the least significant eight bits are exactly the same as

the least significant eight bits of the number 259 that we're interested.

Now, the most significant bits of the mask are all 0s.

So we're ANDing the most significant bits of 259 with the most significant

bits of the mask.

And any time you AND a number with a zero, you get a zero.

So you notice in the results, in the result at the bottom, the most significant

eight bits are all 0s, even though that wasn't the case in 259.

259 actually had a one in bit position of eight.

But because you ended with that zero on the mask it got zeroed out.

So all the most significant eight bits are all zeroed out in the results.

And what this effectively does is allows us to focus on only the bits we want.

So we put ones in the least significant eight bits,

because those are the only bits that we're actually interested in right now.

Because with our EE prompt we're only going to write one byte at a time,

one eight bit chunk at a time.

So this masking you create the mask where you put ones in the bits and

the mask bits you're interested in.

Then you perform a bit wise and, and you see that ampersand that's right there.

That's how you do bit wise and.

You do, you perform bit wise and

between the number and the mask in the end you get a number a result where

only the bits of interest are highlighted by the mask as ones.

And the masks are the ones that you get in the results.

So, what we've done, is if we look at the result,

the least significant eight bits, are the ones that we want.

And now we can take those least significant eight bits, and

just write them to EE prom.

5:02

Now, this example shows us how to get at the least significant eight bits.

We can also- we'll also want to look at the most significant eight bits, right?

So, how do we do that?

So, there are a couple ways to do it, but here's the way we're gonna do it.

We've got 259, again, up at the top.

This time, we want to focus on those bits that we have in blue,

there, the most significant eight bits.

Which are all zeros and then a one.

So first thing we're gonna do is take the number 259 and right shift it eight, okay?

So what does right shift mean?

Right shift means just take the bits and shift them over one, okay?

But we're gonna shift it over eight.

So we're gonna take every bit in a position in 259,

we're gonna shift it over eight positions.

Now what this effectively does is that all those bits

that are in the least significant eight bit positions, they disappear, right.

Because if you shift them over eight bits, they're outside of the number.

But what it does do is that the most significant eight bits

are now the least significant eight bits.

And you can see that we have 259 at the top, we have those highlighted blue.

Now, if you look at 259 right-shift greater than, greater than eight,

right shifted eight those same blue numbers have shifted over eight.

So, the most significant eight are now the least significant eight.

6:12

And the most significant bits are all zeroes now, because when we did the right

shift, when you're shifting into the end, you shift zeroes in.

So you get zeroes in high bits.

So, now we've put the bits we're interested in in the low eight positions,

and then we perform our same bitwise mask again.

We verified AND with the mask.

So we use that same mask we use in the last slide

which allows us to focus on the least significant eight bits.

And we use that same mask and we get the result.

So in the final result you can see that what was originally the high eight bits.

The most significant bits in 259 they've been shifted to the least

significant eight bits.

And then by doing the masking we always see in the final result,

are the least significant eight bits,

which are actually the most significant eight bits of the original 259.

And then all the high bits are zeroed out, because of the masking operation.

And then at that point, you can take that number and do an E Prime write,

and write that byte into the EE Prime.

And you'll write the byte correctly.

7:12

So here's a little program that shows that going on.

We start off with an integer.

We'll call this bigData.

Some integer, 16 bits that we're interested in, okay.

And we wanna write this into the EEPROM into an address.

Then you got a byte we'll call littleData.

What we're going to do is make littleData one byte of bigData.

So we're gonna take big data and first write one byte of it as little data and

then another byte.

So in the set up we say little data equals big data and oxff.

So what does that mean?

That is performing the bid wise and with the mask so oxff is my mask.

Now that notation is called hexidecimal notation right.

A hexidecimal notation, we didn't go over it but hexadecimal is base 16.

And at base 16 you have 16 digits.

Zero through nine plus a, b, c, d, e, and f.

Okay, so, a is ten, B is 11, so on, and F is a 15.

So notice that is OX, OX means hexidecimal, and

the actual value in hexidecimal is FF, and F is a 15, which in binary is all ones,

is one, one, one, one, F is just one, one, one, one.

So if I say FF that is one, one, one, one, one, one, one, one.

That is eight ones in a row.

That is exactly the same as the mask that we used before.

All zeros, and then eight ones.

So 0xFF is the same as the mask that we used on the last two slides.

So this is just a shorthand.

Now people, you should be used to this shorthand a little bit,

because this hexadecimal shorthand is commonly used.

It just makes numbers shorter, right?

Rather than drawing 16 bits, I can just draw these Fs.

0xFF is much shorter.

So it's a common shorthand you'd be using when you're programming IoT devices.

9:04

Okay, so big data and 0xFF, that does the bitwise masking.

The bitwise and with the mask 0xFF.

And so now littleData is gonna be the least significant byte.

And then we can just write that, EEPROM.write to location zero littleData.

Then now I say littleData equals bigData right shifted eight, and

then masked, right?

So now littleData is equal to what was the most significant byte inside bigData,

and then we'd write that.

EEPROM.write(1, littleData).

So note that we are writing the next byte into the next address,

which we have to do right?

Cuz each address only takes one byte.

So what we've done is we've done what's called little endian ordering, meaning

we took the least significant byte and wrote it into the lowest address, zero.

And we took the most significant byte, and wrote it into the higher address.

One.

That's called Little Endian ordering.

We could've done it the other way.

We could've said well I'm going to take the most significant byte and put that in

address zero and then the least significant byte, put that in address one.

That's called Big Endian ordering, and we can do it either way.

And different hardware architectures,

different machines, do use different ordering.

Some machines are Little Endian, some machines are Big Endian.

And you need to be aware that when you're working with a machine.

But it's up to us as we're writing all the code we can write it however we want to.

But just sorta of a side note there, this is little Endian ordering.

Thank you.

[MUSIC]