I'm going to show how to use masking to focus on a certain subset of the bits in a big number, to focus on them and eliminate the rest of the bits. So when I say eliminate, set them all to zeros, so that they are not affecting any computations, okay? So in this example, I got the number, in decimal, 259. Now you can see there's three numbers, three rows here, right? Look at the top row, 259. What you see in there, it's a sequence of 16 bits, zeros and ones. And that row underneath the 259, that represents 259 in binary, okay. And notice, it's a 16-bit representation. Now if I were to look at this 16-bit representation, chop it up into two bytes, to 8-bit bytes. The high byte is all zeros and a one, see that one that I have highlighted in blue there and the low bits, the bits all the way on the right, the low byte there. That's six 0s and then two 1s, okay? So there's a high byte, which is the number 1, all 0s and then 1. And there's a low byte, which is six 0s and then one 1, which is a 3 like and if you would represent in decimal. So that's 259, that whole 16-bit number and 259 has two bytes. Now let's say, I'm only interested in one of these bytes at a time, I'm doing some arithmetic and the reason why this would happen is because remember in Arduino, the Arduino is a 8-bit with microprocessor, microcontroller. Since it's only 8-bits, your arithmetic is 8-bit, so if you want to do addition, multiplication, something like this, you have an 8-bit adder, and 8-bit multiplier, right. So you can't necessarily do operations on 16-bit numbers. So you might have to add the low bits, then add the high bits, something like this. So there are many cases where if you have a bigger number like this, you want to just focus on some subset of the bits and hide the rest of the bits. So in this case, let's say, we want to look at the low byte, we want to highlight the low byte and ignore the high byte of that 259. So what we do is we make a mask, now notice that middle row, that's the mask. Now my mask, you can see, is 8 zeros and 8 ones, the low byte are all ones. So that low byte, where those ones are, those are telling us which bits inside that bigger number, 259, which bits we're actually interested in and which we're not. We're only interested in the low bits, so we made all the low 8-bits equal to 1, and that number is 255. So if you just have the result eight ones like that, that's the number 255. But that is the mask, okay. All of the low bits are the ones that we're interested in because I said, we're interested in the low byte of 259. Those bits are ones in the mask, and all the rest of the bits is zeros. So what we would like to do is we need to, given that mask and given the number 259, we want to get a new number which only has valid values for those bits that are highlighted whose mask bits are 1. Okay, so in this case, the low bits. We want the low bits to keep their value that they have in that top number 259 and the higher bits we want them to be zeros. We want to ignore them. So the operation that you perform with the mask is called a bitwise AND. So, here's what a bitwise AND is. An AND is a basic AND operation. It's where you take a zero and one, true, false, zero, one. And the result is only one if the two inputs are one. So 1 AND 1 is 1. But 1 AND 0 is a 0. 0 AND 1 is a 0, 0 AND 0 is a 0, right. So the only way an AND is going to come true, is going to come one, is if the two inputs are ones. So we're going to do AND and AND operation, but we going to do it in a bitwise fashion. So what that means, is let's take that top number 259, and that mask below it, 255, right? And you can see that each one of those has 16-bit positions, the least significant bit position, we'll call that bit 0. All the way up to the most significant bit, bit 15. And note that bit 0, just so you're aware how I'm counting, bit zeros all the way on the right side. And bit 15 is all the way on the left side. So what we're going to do is perform an AND operation with the corresponding bits in those top two numbers. So if we take 259, bit 0, that's a 1. We take 255, bit 0, that's a 1. We AND those two, we get a 1. So in the bottom row, which is the bitwise AND result, the bit 0 is a 1. Now then say you take bit 1 of 259, that's a 1, bit 1 of 255, that is also a 1. So you AND those together, 1 and 1, you get a 1, bit 1 of the result is a 1, fine. Now, then let's take bit 2. Now bit 2 of 259 is 0, and bit 2 of 255 of the mask is 1. A 0 and a 1 is a 0. So bit 2 of the results at the bottom is also a 0, okay. So this is a bitwise AND. You just do that AND operation 16 times. Once for every pair, every corresponding pair of bits. And you get what's at the bottom, 259 and 255. And notice that the AND operation is an ampersand, but it's a single ampersand. This is important later, because when you write your code, a single ampersand is different than a double ampersand. Ampersand ampersand is a regular AND operation. But a bitwise AND, which is what we're doing here, is a single ampersand, like you see there. So the point though, is that at the AND, if you look at the result, the bottom result of the ANDing with the mask, all the high bits are zeros. That's because the way an AND operation works, is if you AND anything with a 0, you get a 0. And note that in our mask, we made all the high bits zeros, so all the high bits AND results became zeros. So we masked out all the high bits, that's what I would say anyway. So the low bits, if you look at the results at the bottom, the low 8 bits are the same as the low 8 bits of 259. But the high bits are all zeros, that's what the masking did. So we used a mask, we did a bitwise AND with the original number, and now all we have left are the low bits. The bits that were highlighted by the masks. The bits where the masked value was one. The bits where the masked value was zero, they were all zeroed out. That is the point of masking, to focus on a subset of the bits, whichever bits you're interested in, in this case the low bite. But that's what masking is for. So this is how you would get, using this mask, you could look at just the low bits of the original number. Now what if you want to look at high bits as the original number, right? So there are a couple of ways. You could use a mask two and do something with that, but there's no need. Say, we got a 259 up there top row, right, and I am highlighting the high bits this time in blue, those are the bits that we are interested in. Now we want to just look at those high bits. Now what we can do, easiest way to do this is to use, in this case, a right shift operation, so see we say 259, greater than greater than 8. So that greater than greater than symbol, that is a right shift, and greater than greater than 8 means we take the 259 and we right shift the number 8 bits. So every bit position gets shifted over 8 times, so the high 8 bits are now the low 8 bits, right, they get shifted over 8. Now then the question is, what happens to the high bits in the result, right? Well, and then you do this operation, you shift in a 0 on the left hand side. So the high 8 bits of the 259 got moved to the low 8 bits. And you got zeros all in the high 8 bits. So now we have the situation we wanted, where the high bits that we were interested in, they are now present in the result in the low bits, but the high bits of the result are all zeroed out, right, which is the same as what we got before. So now the bits that we are interested in are now the low 8 bits of the result and the other bits are all zeros. We didn't use mask into this. We didn't need a mask, we just used right-shift to do it but we got the same effect, okay. So the effect was, the bits we're interested in are in the low bits and they are valid and the rest of the bits is zeroed. So here's a little example of how you might write an integer to an EEPROM, you do it one byte at a time, okay. So just to look at the setup, we've got littleData, this is just a byte that I have, littleData = bigData, bitwise AND with FF. Okay, so FF in hexadecimal, hexadecimal's a common format to represent this. That's 8 ones, that's 1, 1, 1, 1, 1, 1, 1, 1. Remember the mask that we had? There was 8 ones in the low bits and then 8 zeros in the high bits. That's 0xFF. Why do I do it in hex, hexadecimal? Because hex is compact, okay? I only need two characters to represent 8 ones, okay? So that's why people use hex. It's a convenient way to represent binary numbers. So that is a bitwise masking, that first line, that littleData = bigData AND 0xFF, I'm doing the masking to get the low bits that I'm interested in. And then I do the write of that data. Then, if I want to look at the high bits, I can take the bigData and write shift it 8 to bring those high 8 down to low 8. And then I do a write again, the same write. And this time, I'm writing the high 8. And notice, I'm writing them to different directions. So the first write, I wrote to address 0, second write, I wrote to address 1. So I wrote the least significant byte to address 0, the most significant byte to address 1. And this is called Little Endian ordering, just for your information. Where we take the least significant byte and put it in the lowest address. Notice, we could have done it the other way, right. We could have take the least significant byte and put it in address 1 and put the most significant byte in address 0, if I wanted to. That'd be called big endian ordering and we use little endian and it was a completely arbitrary choice. Thank you. [SOUND]