Data Types in C...
Today we are going to learn about a fundamental Data Type called integer. we have various data types to study. And today we begin our session with integer Data Type. Recall from . introduction to variables. Integer can take either 2 bytes of memory space or 4 bytes of memory space, depending upon the machine you're working with.
we know 1 byte is equal to 8 bits, therefore 2 bytes equals to 16 bits and 4 bytes is equal to 32 bits. More the size more content it can hold. Obviously, if you want to know the size of integer programmatically, then you can by using "sizeof" operator.
"sizeof" is a unary operator that helps us in determining the size of any data type that we provide to it. As you can see here in my computer, "sizeof" integer is actually equal to four bytes as the output is 4 of this "sizeof". I provided the output with the help of this printf function.
Note:"sizeof" is a unary operator and not a function. You might think of it as a function, but it is not a function. Now let's define "Range". Range is nothing but upper and lower limit of some set of data. For example, here you can see this set consists of some values {0, 1, 2, 3, 4}. If you want to determine the range of this set of data, then you have to determine the minimum value as well as the maximum value.
Here, you can see the minimum value is 0 and the maximum value is equals to 4. Therefore, the ranges from 0 to 4. As you can see in this set of data, there is no value which is less than 0, and there is no value which is greater than 4. And that is what the definition of "Range" is.
Now, before diving deep into the concept of range of integers, it is better to get into some prerequisites. And one of the prerequisites that we have to pass through is knowing Decimal number system. In our day to day life, we use Decimal number system for many purposes. Maybe for counting purpose, or maybe when we need to quantize some information like 2 apples, 30 bananas, and so on. Decimal number system is human understandable number system. and is also called as base 10 number system. "base 10 simply means the range of number that we are available with is from 0 to 9.
Let's take one example of a decimal number. We pronounce this number as five hundred and sixty-eight. Instead of simply saying 568. Have you ever wonder why do we pronounce it like that way? Because in reality, we have numbers ranging from 0 to 9. And if you want to represent such a large number, we have to multiply each digit with their respective place values. Here place value are starting from the rightmost end, and it is 10 raise to the power 0, then 10 raise to the power 1, then 10 raise to the power 2, and so on.
When we multiply 8 with 10 raise to the power 0, we get 8 only. When we multiply 6 with 10 raise to the power 1 we get 60. When we multiply 5 with 10 raise to the power 2, we get 500. And now, in order to get the desired result, we add them up to get 568 as our result.
Isn't that easy? Okay! But this is not something that machine could understand. It has its own number system called Binary number ssystem.Binary number system is machine understandable number system, and is also called as base 2 number system. Means range is from 0 to 1. Only two numbers. As you can see, here, I've written a four bit data 1 1 0 0. In this case, also each digit must also have some place values associated with them. Because binary number system has base is equals to 2. Therefore starting from the rightmost end we have 2 raise to the power 0, 2 raise to the power 1, 2 raise to the power 2 2 raise to the power 3 as their place values. And if you want to represent this number in decimal, then you have to follow the same method that we had adopted in case of decimal number system i.e multiplying the digits with their respective place values, and finally add them up.
As you can see here, if I multiply 2 raise to the power 0 with 0, I get 0. 2 raise to the power 1 with 1, I get 0. 2 raise to the power 2 with 1 I get 2 raise to the power 2 which is equals to 4. 2 raise to the power 3 multiplied with 1 I get 2 raise to the power 3 and that is equals to 8. By adding all these numbers, we get 12 as our answer.
Now what would be the range of 4 bit data? As we know, know to calculate range of any number, we have to know its minimum, as well as maximum value. For 4 bit data, minimum value is equals to 0. When all digits are 0. And maximum value is equals to 15 when all digits are equals to 1. You can get this value by using the same method by multiplying each digit with their respective place values and finally add them up. In this case of maximum value you're multiplying 2 raise to the power 3 with 1, then 2 raise to the power 2 with 1, 2 raise to the power 1 with 1 and 2 raise to the power 0 with 1 and finally add them up to get the answer 15. And here because all of them are zero multiplying it with their respective place values would finally give the answer is equals to 0.
Therefore, range of all the data is from 0 to 15 only. But there's one more formula available to calculate the maximum value. And that is, 2 raise to the power n minus 1. Suppose you have a 32-bit data, then adding all these place values multiplying with their digits is quite difficult, then this formula is quite handy to apply. Here, by replacing n is equals to 4 for 4 bit data, you get 2 raise to the power 4 minus 1 which is 16 minus 1, and that finally gives the values equals to 15, which is our maximum value.
Let's calculate the range of integer. If in your machine, the size of integer is of 2 bytes, then the unsigned range is from 0 to 65 535. By applying the same formula for calculating the maximum value. By replacing n with 16, you get 2 raise to the power 16 minus 1 which is nothing but 65 535. Usually, you also have to represent the negative values. And that we can do with the help of 2's compliment representation. We actually have three different representations to represent negative numbers, and they are signed magnitude, 1's compliment and 2's compliment. But in most of the computers 2's compliment representation is used. 2's compliment range is from -2 raise to the power n-1 to +2 raise to the power n-1 minus 1.
If you want to know more about 2's compliment representation and other representations, then you can refer the Neso Academy playlist of "Digital Electronics" here on YouTube. Here, you can see -2 raise to the power n-1. By replacing n is equals to 16 I get 2 raise to the power 15 which is -32 768 and +2 raise to the power n-1 i.e. 2 raise to the power 15 minus 1 that gives 32 767.
On the other hand if your PC supports 4 bytes integer, then Unsigned range is from 0 to 4 294 967 295. By applying the same formula for the maximum value, Signed range can be obtained by using this 2's compliment range. That's it. See you later…..
कोई टिप्पणी नहीं:
एक टिप्पणी भेजें