Limitation of Integer type in programming languages

If you are a programmer, then you may know that the range of integer types are limited. For example, for a 64-bit compiler, c++ int type occupies  4 byte. And the range of int is -2,147,483,648 to 2,147,483,647. Suppose you build a program that takes an integer, adds 1 to it and then returns the output to the user.

 #include<iostream>
 int main()
 {
   int num;
   std::cin>>num;
   std::cout<<(num + 1);
   return 0;
 }

Everything is working fine as expected. The user gives a number and the program adds 1 to it in a second. But what if the user enters the highest number that the int type can take? In this case user enters 2,147,483,647. Output will be -2,147,483,648 . You know it right? But have you ever realized why this is happened? Why did the addition of the two numbers give us the highest negative number int can take? Let’s find it out –
But before talking about 32-bit integer type ,let us think of a smaller fictional type say small which can contain any 5 bit signed number. There is no such type of integers in programming language. We assumed it for simplicity.
small values
binary representationdecimal values
000000
000011
000102
000113
001004
001015
001106
001117
010008
010019
0101010
0101111
0110012
0110113
0111014
0111115
10000-16
10001-15
10010-14
10011-13
10100-12
10101-11
10110-10
10111-9
11000-8
11001-7
11010-6
11011-5
11100-4
11101-3
11110-2
11111-1
As shown in the table, the highest positive integer small can take is 15. And  the highest negative integer is -16. Here (10000)₂ to (11111)₂ is reserved for negative numbers which is actually the 2’s complemented form of the positive numbers.
If you saw my previous article, you know that every negative number is stored as 2’s complemented form. For example, binary representation of 1 is (00001)₂ and binary representation of -1 is 2’s complement of (00001)₂ i.e. (11111)₂, binary representation of -15 is (10001)₂, -16 is expressed as (10000)₂ and so on. small is signed small by default. unsigned small can contain 31 as the maximum number. unsigned small cannot take negative number. So, in that case, (11111)₂ will be 31 and (10001)₂ will be 17.
So, when we sum 1+15 (where 15 is the largest positive value of small), binary summation takes place as described below –

We got -16!! This is not expected. But if you see the binary summation carefully then you will realize that (10000)₂ is -16 as shown in the previous table. Exactly same thing is happened for int type. This limitation is not only for signed integers. Unsigned integers also face same problem. For example, if you sum 1+ 31 in the case of unsigned small you will get 0 instead of 32! Let’s have a look at the binary summation

But unsigned small can take only 5 bits. So, the MSB (in this case 1) is dropped. Thus, the result is stored as (00000)₂ where 1 is ignored.
So now you know the reason that worked behind the first case. If you have any doubt, comment below. I shall try to clear your doubt.

Comments

Popular Posts