 Alright, Mr. Gibson here with your next lesson in cryptography, and today we are going to be focusing on operations that we can perform with binary numbers. So the first one we're going to look at is binary addition. So it's just adding the same two numbers that we always know how to do, but we need a different algorithm to do that with binary as opposed to the one that we're used to doing with decimal numbers. So we're going to look at the characters capital T and capital G, and there's their 8-bit ASCII representation, but T and ASCII corresponds to the number 84 or in binary 01010100. And G corresponds to the decimal number 71, which is also 0100111 in decimal. And our goal is to add these two numbers. We should know, just by knowing that 84 plus 71 is 155, what our final answer should be, but what we're looking to do is determine how can we compute that using only the binary representations. And to do that, let's just refresh real quickly the algorithm we'd used for decimal representations. This is going way back to elementary school here, but we're adding two-digit numbers together. So we kind of had this algorithm that you probably learned where you stack those decimal numbers vertically on top of each other. You'll start with the ones position, and you'll add the four and the one to get a value of five. And then we move on to the tens position. We'll do the eight plus the seven to get a 15, and the way that we write that is we write the five down below, and then we carry the one, meaning we're going to add a value of one to the next position. And then we'll sum the digits in the hundred spot. Now the one that we carried happens to be the only digit in the hundreds position in all of these numbers. So when we sum that column, we also get a value of one. And there's our final result of 155. Now it turns out we can reuse this algorithm very similarly with binary numbers. Just remember that we only have the values zero and one to work with. So here's the same two numbers represented in binary. Again, we're going to start in the lowest position, the ones position, and we'll sum the values in that column, so zero and one sum to one. And we'll move to the next position to our twos position, zero and one also sum to one. We'll move on to the next position, the four's position, one plus one is going to give us a total of two, which we represent as one zero. So we're going to write the zero down below, and the one we're going to carry over to the next position. In binary we call that kind of an overflow into the next position, so kind of a computer science specific term for that carrying of the one. So we'll move to the next column, which is our, let's see, eighth position, and we've got one plus zero plus zero is one. And we move on to the sixteens and so on. So as we move down column by column, we can sum up each position, carrying the one when we need to, until we've moved through all eight bits. And we get to the final result of one zero zero one one zero one one, which yes is one fifty five in decimal. So that algorithm that we're used to using for our decimal representations works just as well in binary and we get the same result. The only thing about when we're working with ASCII characters is that when we sum two things together, there's no guarantee that the result is actually going to represent a visual character or even a character at all, or that the result of the summing two eight bit numbers is also going to be an eight bit number. If those two numbers were sufficiently large and we summed them, the result might be a nine bit number. So plain old addition when we're working with ciphers will not be used very often because of that fact. We want predictability when we sum two eight bit numbers, we usually want that to be an eight bit number so we can convert it back to a character. So this is another operation we'll look at in just a minute that guarantees that. First, let's take a real quick look at how we can do binary addition in Python or really just addition in general in Python. The first command here, the eighty four plus seventy one shouldn't be new to you. We know we can do arithmetic and decimal in Python. But here's how you can add those same two numbers with their different representations. So we've seen in an earlier lesson that we can prefix a binary number with zero B to indicate this is a binary number. So there's the exact same two binary numbers we just added by hand that are being calculated using Python. And that's not unique to binary. We can do the same thing with hex. So eighty four is the hexadecimal number five four. The seventy one is a hexadecimal number four seven and using the zero X prefix on each of those numbers we can add them together at the command line as well. You might not have to do this, but it's just interesting to see that Python has this built in natively. It'll always return the result of that calculation back to you as a decimal. So let's look at our second binary operation. This is known as the exclusive or operation also shortened down to just be X or XOR. And this is an operation where we can combine two binary numbers and we're always going to get the same number of bits as we started with. So these are two eight bit numbers. This algorithm will result another eight bit number every time. And the way this works, you'll see we have a new symbol here. It's a plus with a circle around it. That's the operator for the XOR operation. And we start the same way as we look in the column of the lowest position zero and one. And what we're going to look at is, are they the same or are they different? The XOR operation is going to return a one if these two values in the two numbers in that position are different. And they're going to return a zero if they're the same. So if we look in our one spot of these two eight bit numbers, we got a zero and one of the positions and a one and another. So those are different. So the exclusive or results with a one. The next position is also zero and one. So the result is a one. The third position here, our four spot has two ones. So those are the same. So those are going to return as a zero. Two zeros will also return a zero, a one and a zero, a one, and we can move down the line. And this operation results in a binary number that represents the decimal 19 or in binary 0, 0, 0, 1, 0, 0, 1, 1. Python can do this operation as well. It uses the carrot symbol, one that we typically might think of used is used for exponents. But remember in Python, exponents is star star. The carrot operator here actually does that binary X or operation behind the scenes, even if you feed it to decimal numbers. So you can see 84, carrot 71 returns 19, just like our by hand. You could give it the binary representations and it will still give you 19 as a result. And yep, even the hexadecimal representations, you can use that X or operator to get a result of 19. And those are the only two binary operations that we're going to need to be familiar with in this course. There are several others out there, but the X or is really going to be the big operation that we use for cryptography with binary representations of characters. So that's it for today. Thanks for watching. We'll catch you in the next one.