 Hello and welcome. This lecture is about how computers internally represent integers. Here is a quick recap of some relevant topics we have already studied. We have seen the architecture of a simple computer and we have also seen that information is stored in terms of binary digits or bits which are usually grouped in chunks of 8 called bytes. In this lecture we are going to study how numbers and specifically how integers are represented inside a computer and we are also going to see how in a C++ program we can declare integer variables. Now from the earlier lecture you would recall that a snapshot of our simple computer looks like this where we have different parts of the computer and in each part we basically have sequences of 0s and 1s. So the question is in such a setting how do we represent integers like 56 or minus 37 in terms of 0s and 1s inside a computer. Now when we talk of representing integers the most natural thing that comes to mind is a decimal representation with which all of us are aware. So for example when I write a decimal number like 233 I am really using base 10 which basically means that to interpret this number I take 2 and multiply it by a power of 10 in this case 10 squared then I take 3 multiplied by a power of 10 in this case 10 raised to 1 this 3 is multiplied by 10 raised to 0 and I add all of them to get the value of this decimal representation of the number. So we call it base 10 because this number which we are raising to various powers and multiplying it with the digits here is 10 and in base 10 representation the digits that we need are from 0 to 9. Now there is nothing very special about a decimal representation we could also do this using base 2 in which case we will call it a binary representation and just like in base 10 we need numbers from 0 to 9 to represent an integer in base 2 we need numbers from 0 to 1 which is 1 less than whatever the base is to represent an integer. So here is a binary number 110 please do not mistake this as 110 it would be 110 in decimal representation but in binary representation it is 110 and let us see what this number really represents what 110 represents. So just like in the decimal representation I have got to take the numbers that are appearing here multiply them by an appropriate power of 2 the base is 2 here and then add them up to get the number represented over here. So for example here it is 1 into 2 squared plus another 1 times 2 raised to 1 plus 0 times 2 raised to 0 or 6. So this is the binary representation of the number 6 and if you understood how I interpreted the sequence of 0s and 1s as an integer you would realize that any sequence of 0s and 1s can be thought of as representing an integer. Now that we know how to represent numbers in both binary and decimal notation how do we go from one to the other? So for example I could ask you given this binary number I am going to denote binary numbers in green and decimal numbers in violet. So given this binary number what is its decimal counterpart? So the first thing to note here is that this binary number has 4 bits. So this bit is going to be associated with 2 raised to 0, this with 2 raised to 1, this with 2 raised to 2 and this with 2 raised to 3. So the maximum power of 2 that we are going to use is 3 that is the number of bits minus 1. And then how do we figure out what integer this is in the decimal notation? We take this numbers start with the bits and keep multiplying them by the appropriate powers of 2. So this is 1 multiplied by 2 raised to 3 plus 0 multiplied by 2 raised to 2 plus another 1 multiplied by 2 raised to 1 and the final 1 multiplied by 2 raised to 0 and that gives us the number 11. Note that in this representation which is the decimal representation of the number 11 I am also using the digits 1 and 1. This is the binary representation of the same number 11 in which I am also using the numbers 1 and 0. However this representation is very different from this representation. This is a base 10 decimal representation. This is a base 2 binary representation of the same number 11. So if you look at 1011, this bit which is associated with the highest power of 2 is called the most significant bit or MSP. This bit which is associated with the least power of 2 which is always 2 raised to 0 is called the least significant bit or LSB. So we saw how to convert a binary number to a decimal number. Let us see how we might convert a decimal number to a binary number. So given 23 written in decimal what is its binary counterpart? So the way we are going to do this is since we are interested in binary numbers we are going to repeatedly divide this number by 2 and collect all the remainders as we go. So I first divide 23 by 2, I get a quotient of 11 and a remainder of 1 and we will see that this remainder of 1 will be our least significant bit in the binary representation of 23. Now what does this mean that 23 divided by 2 give me a quotient of 11 and remainder of 1. It really means that 23 can be expressed as the quotient times the divider 11 times 2 plus the remainder. Now what do we do with this quotient 11? We repeatedly divided by 2 again and we get a quotient of 5 and a remainder of 1 and now I could express 11 as 5 times 2 plus 1 and if I did that in place of this 11 if I substituted 5 times 2 plus 1 I would get this expansion of 23 and if I expanded out further I would get this representation. And here you see now that this one corresponds to the least significant bit which is 1, this one corresponds to the next significant bit which is another 1 and I could keep doing this. I could take 5 divided by 2 get a quotient of 2 remainder of 1 then express 5 as 2 times 2 plus 1 just as I have done here and that way I get this representation of 23 which actually gives me the next remainder. And finally I take the quotient here which is 2 divided by 2 again get a quotient of 1 remainder of 0 and now if I express this 2 as 1 times 2 plus 0 this is the expansion of 23 I get which finally gives rise to this expansion of 23. Now where do I stop? I will continue until I get a quotient of 0. So I divide 1 with 2 get a quotient of 0 and it is only then that I stop I get a remainder of 1 and this becomes my most significant bit. So as you have seen that we took the decimal number 23 repeatedly divided by 2 kept collecting the remainders in order from the least significant bit to the most significant bit and that gives us the answer 10111 as the binary representation for the decimal number 23. Now so far we have talked about representing unsigned integers. So how do we represent signed integers like minus 1 or minus 23? Well the most simplest way to do it might be to just treat one of the bits as the sign bit. So we could choose the MSP to be the sign bit and if this bit was 1 we could treat the number as a negative number otherwise we could treat this number as a positive number. So here if we consider integers represented using 3 bits I have placed all these 8 valuations of 3 bits along a circle marked in color the most significant bit the ones marked in red are the ones where the most significant bit is 0 the ones marked in blue are where the most significant bit is 1. So in our notation these would represent positive numbers these would represent negative numbers and this is what I would get. So all of the numbers with MSB0 are positive and then the black part just gives me the value of the number 0123 all of these with MSB1 are negative and then the black part of the number of the binary representation just gives me the value of the number the magnitude of the number which is 0123. So you see that the values represented in increasing this way 0123 and also increasing this way minus 3 minus 2 minus 1 0 and in fact we have 2 representations of 0 here. So this is kind of wasteful and so what we might want to use is another representation which is called the 2's complement in which we still consider the MSB to represent the sign of the number and so in our circle along which we have placed all the valuations of 3 binary digits we would still have these 4 representing positive numbers these 4 representing negative numbers. However we are now going to treat these positive numbers as 0123 and instead of these being minus 0 minus 1 minus 2 minus 3 we are going to now consider minus 4 minus 3 minus 2 minus 1. So effectively we are starting from here continuing to increase minus 4 minus 3 minus 2 minus 1 0123. So we have one complete sweep over here in which all the numbers represented increase and of course finally when we go from here to here we would go from plus 3 to minus 4 the largest integer represented to the smallest integer represented. So that is the wrap around that must happen. So you see that in this way of representing signed integers we have not been able to represent 8 numbers minus 4 through 3 and there is only one representation of 0. This is also called the 2's complement representation and one might ask if there is an easy way to figure out given such a binary sequence what number does it represent in 2's complement? We could draw the entire circle like we did here and try to figure out what number it represents but is there an easier way to figure out? It turns out that there is. So you first look at the MSP and the MSP here is 1 which means the number is a negative number then to get the absolute value of the number you ignore the MSP and look at the rest of it. So in this case it is 0 triple 1. The first thing you do is flip every bit in this part which you obtain after ignoring the MSP. So 0 triple 1 with every bit flip becomes 1 triple 0. This process is also called taking the 1's complement. So 1 triple 0 is the 1's complement of 0 triple 1 and this as you would recognize represents the decimal number 8 and now what we do is we take this number and add 1 to it to get 1 0 0 1 or the decimal number 9 and this is now called the 2's complement of 0 triple 1 and this gives you the final absolute value of the number that was represented here. So the absolute value here is 9. The number was a negative number. So 1 0 triple 1 in 2's complement represents minus 9. Now in C++ program how do we go about saying that something is going to store integer values? We use this keyword int which represents the integer data type. I have highlighted the keyword representing the data type in red and C++ actually allows different kinds of integers. For example I could have short integers, long integers, long long integers, unsigned long integers and what all these different qualifiers do is they tell us how many number of bytes are to be used to store the value of the integer. In a short integer I use 2 bytes and standard integer I use 4 bytes. In a long long I might use 8 bytes and they also tell me whether I should treat the integer in 2's complement or not. So all integers unless explicitly specified otherwise are signed 2's complement representations. If you specify something as unsigned then of course it is an unsigned integer and it is not hard to see that the maximum and minimum values will depend on the number of bytes and on whether you are treating it as a signed or unsigned integer. For example if I take a standard integer which is signed 4 bytes then minus 2 raised to 31 is the smallest number that you can represent and plus 2 raised to 31 minus 1 is the largest number you can represent. Whereas if I take unsigned long long which is 8 bytes I can represent values from 0 through 2 raised to 64 minus 1. In C++ you declare a variable of integer type by putting the type name here int then putting the variable name my marks or you say unsigned short int that is the type name with the qualifiers and then the variable name. You can also represent constants integer constants in C++ they could be represented in the decimal notation which is the usual notation that we always use a sign followed by digits from 0 to 9. The only thing you have to notice that you cannot start it with a 0 unless you are representing the value 0 because if you do start with a 0 it represents a constant in the octal notation which is basically base 8 notation. So if I say 0 372 it is not the decimal number 372 but it is the octal number 372 and you can work it out yourself what it represents in decimal and in binary. Similarly I could also use hexadecimal in which case I would have to prefix 0x and in hexadecimal which is basically base 16 I need numbers from 0 to 15 but we only have the numeral 0 to 9 so we use a through f to denote 10 through 15. So if I have a number like this prefixed with 0x it is a hexadecimal number and using the usual expansion in base 16 I could figure out what number it is in decimal and what number it is in binary. As a declaration I could say that something is a constant integer so here this is the constant name scale factor is a constant integer and this is the value of this constant and when I say constant then the value of this cannot be changed during programming execution. So here is a summary of what we have studied today we have looked at binary representation of integers conversion to and from decimal to complement representation and C plus plus declaration of integers. Thank you.