Range of Data-Types – C++ – Lecture 7
In the previous lecture, we discussed the size of data-types. Building on the concept, in this article, we will understand another exciting concept, the range of data-types in C++!
Calculation of the Range of Data-Types
Alright, to begin with, let us understand what is the range of numbers that can be represented by 3 bits(binary system, recall that we can create any number using the sum of powers of 2).
0/1 0/1 0/1, now, what is the maximum number that can be represented by 3 bits?
7, because the maximum number will have all the bits set to 1 i.e., 1 1 1,
(1 x 20) + (1 x 21) + (1 x 22) = 1 + 2 + 4 i.e., 7 (we always start from rightmost bit).
What is the minimum number that can be represented by 3 bits? 0 i.e., 0 0 0. So, the range of numbers represented by 3 bits is 8 (0, 1, 2, 3, 4, 5, 6, 7) or
2number of bits = 23 = 8.
The entire range in binary form is shown below,
0 0 0 -> 0 (0 + 0 + 0)
0 0 1 -> 1 -> (1 x 20) + (0 x 21) + (0 x 22) -> (1 + 0 + 0)
0 1 0 -> 2 (0 + 2 + 0)
0 1 1 -> 3 (1 + 2 + 0)
1 0 0 -> 4 (0 + 0 + 4)
1 0 1 -> 5 (1 + 0 + 4)
1 1 0 -> 6 (0 + 2 + 4)
1 1 1 -> 7 (1 + 2 + 4)
Another way to figure out the 8 numbers can be, ‘How many permutations can you create using 0 and 1’? 4, observe this,
(0 0), (1 1), (0 1) and (1 0). Now, add 0 and 1 in front of these permutations, you get the 8 numbers(but not in order),
(0 0 0), (0 1 1), (0 0 1), (0 1 0) and (1, 0 0), (1 1 1), (1 0 1), (1 1 0).
Range of Char
Using the above concept, let us calculate the range of char data-type. We already know that char is 1 byte which is equal to 8 bits(there are 16 bit and 32 bit char too – UTF i.e., unicode transformation format). Here, char has a range of 28 = 256(0 to 255).
Now, you might wonder, char does not store numbers(it stores ‘a’, ‘A’, ‘$’ etc.), so how are we able to figure out its range?
This introduces us to the concept of ASCII values. ASCII table has 256 values. In ASCII table, every character in char has a number associated with it. Important ones to remember are:
- The digits 0 to 9 with values from 48 to 57.
- The capital letters A to Z starting from 65 and ending at 90.
- The small letters a to z ranging from 97 to 122.
Now, we are in a position to discuss the difference between signed and unsigned char. By default, char uses unsigned values.
To understand signed char, just remove 1 bit from the 8 bits of char,
+/- 1 1 1 1 1 1 1, so what is the range now?
The reduction in 1 bit reduced the range to 27 i.e., 128. Signed char still has 255 values but now it ranges from -128 to 127 (0 is included).
Range of Data-Types in C++
Let us figure out the ranges of all the data -types we discussed.
- bool -> 1 byte(but it only stores true or false)
Range of Int and its modifiers
- short -> 0 to 216 – 1 i.e., (0 to 65,535)
- int -> 0 to 232 – 1 i.e., (0 to 4,294,967,295)
- long -> 0 to 264 – 1 i.e., (0 to 18,446,744,073,709,551,615)
- long long -> same as long(mac)
- signed short -> -215 to 215 – 1 i.e., (-32,768 to 32,767)
- signed int -> -231 to 231 – 1 i.e., (-2,147,483,648 to 2,147,483,647)
- signed long -> -263 to 263 – 1 i.e., (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
- signed long long -> same as signed long(you need not remember these values).
Range of Float and Double
In C++, float has 7 decimal digits of precision(6 digits after point .) while it is 15 for double.
- float -> 3.4E +/- 38
- double -> 1.7E +/- 308
- long double -> same as double(mac).
Now, what does these ranges mean. The range 3.4E +/- 38 means that the range of float is -3.4e38 to +3.4e38.
Going a little deeper, it means,
- The maximum +ve(positive) value float can represent is 3.4e38.
- The minimum +ve value it can represent is 3.4e-38.
- Similarly, the -ve values ranges from -3.4e38 to -3.4e-38.
Same calculation can be done for double. But what does +3.4e38 mean?
3.4e38 is same as 3.4 x 1038. -3.4e-38 is equivalent to -3.4 x 10-38. Cool, now let us discuss the last topic of this lecture.
Macro Constants
With the help of macros, we can use the already defined constant values in our C++ program. They are present in the #include <limits.h> or #include <climits> header(depends on the compiler). Most popularly used macro constants are:
- INT_MAX – constant with maximum value of int
- INT_MIN – constant with minimum value of int(both as discussed above).
I hope this article helped you in learning about the topic you were looking for. If you found this article helpful, share it with your friends and peers who want to learn programming in a fun way!
And I will see you in the next one😀.