 In this video, we'll be discussing different operations that you can perform on binary numbers, both analytically and also with the Python programming language. So we'll start by looking at an addition. We have two seven-bit binary numbers, so take seven digits to represent each of these numbers, and then they have their decimal equivalence here on the parentheses. So we're trying to add the numbers 84 and 71, which are represented as 1010100 111 respectively. And to add these binary numbers by hand, let's just take a quick refresher on how we would add multi-digit decimal numbers by hand, as the algorithm isn't any different. So if we were to add these numbers in their decimal representations, we'd start by looking at the furthest right position in each number, the ones digit. And here we can see four plus one, we can add those vertically to have a result of five also in the ones position of our result. And now we can move our attention to the next digit, the one in the tens position, and we can see that eight plus seven is 15, and the way that we represent that is by writing a five down below, and then carrying over a one into the next position, which is the hundreds digit. And then we would add all of the digits that are in this third column that represent groupings of 100. And we only have the one that we carried over, there are no other digits in the hundreds position on our two numbers. So the total of that column would be one, yielding a final result of 155 that we obtained by adding 84 and 71. So again, this algorithm probably very familiar to you, you've used it many times in your mathematical career, but let's see how we can now extend this to work with other base systems. So here we have the exact same two numbers just represented using binary instead. And we'll use the same algorithm, let's start with the furthest right hand column of digits are ones digits, and we'll sum those together zero plus one is one. And now we'll move our attention to the next digit. This is now not the tens position, but rather the twos position. So our first number contributes zero groupings of two, and the second number adds one grouping of two for a total of one in our twos position in our result in our sum. And we'll do the same thing now to the next position, which would be our fours position, the total of one plus one is two, which is represented by one zero in binary. So we put the zero in our sum and we carry the one up into the next column of digits, which we could then total that column to get a result of one. The next column has a sum of one, a sum of zero. And then lastly a sum again of two, which we would represent as one zero. So we write the zero down below, carry the one up to the next column, and then add that last column. So our final result here is one zero zero one one zero one one, which is the same answer we obtained before only in binary form. Now notice our two numbers that we were adding together needed seven bits to represent. And our final answer needed eight bits to represent. It's not uncommon when you add two binary numbers together that the result ends up requiring more bits to represent the number. This last bit, this new bit that gets added onto the end here, is often called the carry out bit. We'll see that these binary operations are often carried out using circuits and an actual physical piece of hardware, like a computer processor. And some of those hardware devices were designed only to handle a certain number of bits input, and then they will output the same exact number of bits as the output there. But you can pass along that last carry out in a separate channel or a separate wire. So you might hear it referred to that, depending on where you're reading about binary. Not so important in this course, but just wanted to mention it in case you saw it in your own research. As we've discussed in an earlier lesson, Python can just natively add numbers together, regardless of the base that they're in, and they will display them output, the output for you in decimal, unless you specify otherwise. So as a refresher, you can add these numbers together. Remember, you use zero B as the prefix before you start typing in your binary digits, and there's a result of 155. And if you want to see the output formatted as binary, you can add those two numbers and then pass the sum as the input argument to the format function, and then you pass a single character, a lowercase b, as a string, as the second argument to the format function, and it will display all of the bits needed to represent that same numerical output in binary instead of decimal. Now besides addition, there are other logical operations that can be applied to binary numbers, both by hand and by using Python. The first we'll look at is the exclusive OR operation, also formatted as the capital letter X, and then O, and then R. We just equivalent to just addition in modulo 2 on a bit by bit basis for your two numbers. So you might often hear this referred to as XOR addition, or just XOR, or addition mod 2. They're all kind of the same thing. Now this algorithm will feel very similar to the binary addition algorithm that we just looked at, but in some ways it's even easier. We would look at this first column of our ones digits here, and we're going to add them up and then mod by 2. So 0 plus 1 is 1. 1 mod 2 is 1. Now this operation gets its name because the other way to think about doing this is we're going to compare the two numbers, and we're going to result in a 1 for the output and the underneath those two numbers, if and only if those two numbers are different. So an exclusive OR, one of them is 1, or the other, but not both. That's the exclusive part of the exclusive OR. We can try that on the next column of numbers. 0 and 1 are different, so we'll output a 1. We'll move on to the next column. 1 and 1 are the same, so we are going to output a 0. OR, think about that as 1 plus 1 is 2, and 2 mod 2 is 0. Move on to the next column. 0 and 0 are the same, so output a 0, and then a 1, and then a 0, and then a 0. Now besides the addition mod 2, you'll notice the other big difference in this algorithm is that we don't do any carrying of any ones, and that's because each individual column doesn't ever have an output other than 0 or 1. There is no output of 2 or 1, 0. So we can avoid that carrying, and in that case we are guaranteed that any two numbers that we X OR together will contain the same number of bits in their output as the number with the largest number of bits in the input. If you happen to have a number that maybe only needed two or three bits and another number that had six or seven bits, you could always put in leading zeros or pad out the smaller bit representation of the number just from the left-hand side. We'll see an example of that in a little bit. The way you can tell we're doing an X OR, besides the fact that I said we were, is this plus symbol with a circle wrapped around it. This is a logic representation for this operation. So when you see that plus with the circle, you know you're doing an X OR addition instead of the normal traditional addition algorithm, the carry addition. And that answer has a numerical representation of 19, just in case you were curious and wanted to practice on your binary skills. Now, we could do this in Python. We can see kind of a graphical representation on the right side of the screen that shows what happens when you have two 8-bit inputs and you X OR them. But on the left-hand side, we can see the Python code that actually will perform that calculation. We use the carrot key, which is the shift 6 on your keyboard. You can do it with the decimal representation or the binary representation. Python does not care. You'll get the same output of 168. We could also do an AND operation in Python, which will go through your two numbers bit by bit. And for the bits occupying the same position in those two numbers, this operation will return a 1, only when both bits are switched on. So they must both be a 1. So we can go through, we can see it mishmash it there and the graphic on the right, or we can do the algorithm here on the left of the AND symbol, or the ampersand, which is a shift 7 on your keyboard. And again, decimal, binary, doesn't matter. Python's happy to do this operation on that number. And then the last logical operator that we'll look at in this lesson is the OR operation. So this is a little bit different than the exclusive OR operation. The big difference here is that if you have a 1 and a 1, this will be happy to return a 1, whereas the exclusive OR needed a 0 and a 1 in a corresponding position. This one will, this one, the regular OR, will still output a 1 when two bits are set to 1. This uses the pipe symbol, which is kind of shift and then the backslash, the one that's typically near the backspace key on your keyboard, and that'll perform this operation for you in Python. And then some last miscellaneous ways that we can work with Python that might come up in this course. You can always find the how many bits it takes to represent a decimal number by using the bit length function. So if you throw your integer or decimal number inside of a set of parentheses, followed by dot bit underscore length, open, close, print, it'll show you how many bits you would need to represent that decimal number. Now, it's important you use the parentheses. The way Python is set up is that if you have an integer followed by a dot directly, it'll assume that you're actually trying to make a float, whether the integer component and then decimal component. But by using the parentheses, Python will know, okay, you want to use this dot bit length function on the number inside the parentheses. First time we've really seen that syntax before. Next up, we can actually print out. This is another way to print out the decimal, or sorry, the binary representation of a decimal number. So we use print, and then inside the parentheses, before your quotes for a string, you can put the lowercase letter F. This is called an F string in Python. You could go down a whole rabbit hole just on F strings. They don't show up too much in this class, but one thing they do very well is format decimal numbers as binary instead. So the F is short for format. You open up some quotes for your string. You do the curly braces, so like the shift square brackets on your keyboard. You put your decimal number on the left-hand side of that colon. To the right-hand side of the colon, you put the single lowercase letter that represents the base system you're working in, B for binary, X for hex, D for decimal, and so on. And then close up those curly braces and end your string. When you print that out, it'll format the 42 as a binary number and produces a string to do that. Now this F string is very customizable. So say for example, I wanted to pad out that 42 to 32 bits. I wanted to use a lot of extra bits to represent 42. In fact, I want 32 bits total to represent that. To the right of the colon, before we have that lowercase B, you can do zero. Notice it gets coded in black, meaning it's different from those 32 that comes after it. It's saying pad out the number 42 using a zero. And in fact, pad that out. So there are 32 bits in this number. So zero for the padding character, 32 for the number of bits you want this to be when you're done, and B for representing it in binary. So all of that combined gives you that really long representation, a lot of leading zeros to pad that out in order to represent 42, a number that really only needs six bits using 42. Now I skipped over a tried and true one that we learned in previous lessons. You can always convert a string version of a binary number back to its integer representation using the int function. Remember, you just pass it the string version of your binary as the first argument, and then the base in this case two. As the second argument to that function, and it will convert that string back to a number that you can work with mathematically in Python. That is going to do it for our binary operations. So at this point, we now know how to take a decimal number, represent it in binary format. We can do that addition in binary. We can also do our XOR addition, our addition mod two in binary, and assign and use logical operations like and and or using some syntax in Python.