data types

Hi,
I am not understanding the minimal and maximum range of data types.
For example:-
integer variable can store values -32768 to 32767,
character variable can store values in the range of -128 to 127 etc.
Please explain what is that number signifies?


First, that isn't necessarily true. You need to use something like the <limits> library to accurately determine the possible values for the integers.

As for the ranges of those things:
In your case, a char is composed of 8 bits. However, the 1st bit is the sign of the number, i.e. whether its positive or negative. Also, the way binary works, is that you have each column being the 1s, 2s, 4s, 8s, 16s, 32s and 64s, as well as the sign. If you have all the bits (excluding the sign) turned on, you get 127 (or -127 if the sign bit is on). Also, if the sign bit is on but nothing else is, because -0 doesn't make sense by convention the result is -128.

The same goes for an integer variable. In your case an integer is 2 bytes, or 16 bits. Hence, by ignoring the sign bit, it goes from 32767 (215 - 1) to -32768 (- 215).
Last edited on
Hi NT3,

I am not understanding,will you please explain clearly...
What don't you understand?
In your case, a char is composed of 8 bits. However, the 1st bit is the sign of the number, i.e. whether its positive or negative. Also, the way binary works, is that you have each column being the 1s, 2s, 4s, 8s, 16s, 32s and 64s, as well as the sign. If you have all the bits (excluding the sign) turned on, you get 127 (or -127 if the sign bit is on). Also, if the sign bit is on but nothing else is, because -0 doesn't make sense by convention the result is -128.

The same goes for an integer variable. In your case an integer is 2 bytes, or 16 bits. Hence, by ignoring the sign bit, it goes from 32767 (215 - 1) to -32768 (- 215).


Please explain it in different way.
Also ,How can we come to know that data type is of ---bytes by seeing the range of values.
An 8-bit signed char is comprised of 8 bits:
SIGN 64 32 16 8 4 2 1

Hence, the largest possible value:
0111 1111 = positive 64 + 32 + 16 + 8 + 4 + 2 + 1 = 127

Smallest:
1000 0000 = negative 0, but by convention is -128

A 2-byte signed int is comprised of 16 bits:
SIGN 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1

Hence, the largest possible value:
0111 1111 1111 1111 = positive 16384 + 8192 + ... + 2 + 1 = 32767

Smallest:
1000 0000 0000 0000 = negative 0, but by convention is -32768

You can come to know how many bits is composed in a type by seeing the range of values, but knowing the powers of 2. For example, 32768 = 215, hence it must be composed of 16 bytes (including the sign).
One thing I understand about
"integer variable can store values -32768 to 32767" means the integer variable can hold the values only between -32768 to 32767.It cannot take if the user enters other values which is out of range.
For example:-
if the user gives the number x=32768 which is out of range as input.It will not take correct number?

am i correct?
Last edited on
You will get whats called an 'overflow' - the value will loop round. For example, 32768 is 1000 0000 - but that's -32768.
It can be useful to consider cases with a smaller number of bits. Say we look at a 3-bit binary number. Here is a list of all the possible values:
Binary   Decimal
  000       0
  001       1
  010       2
  011       3
  100       4
  101       5
  110       6
  111       7 


That's when treating the value as an unsigned integer. If we consider it as a signed integer, with both positive and negative values, it will look like this:

Binary   Decimal
  100      -4
  101      =3
  110      -2
  111      -1
  000       0
  001       1
  010       2
  011       3


Since there are a limited number of unique patterns available with a given number of bits, clearly it can be used to represent only a limited range of values.

If we try to represent a larger number, some of the higher-order bits would be discarded, there's no place to store them in our 3 bits.

In normal use, the integers we use have a fixed width - though it is possible to write code to give integers of an arbitrary length.
> It cannot take if the user enters other values which is out of range.
> For example:-
> if the user gives the number x=32768 which is out of range as input.
> It will not take correct number?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <limits>

int main()
{
    constexpr int int_min = std::numeric_limits<int>::min() ;
    constexpr int int_max = std::numeric_limits<int>::max() ;

    std::cout << "int_min: " << int_min // -2147483648 on this implementation
              << "\nint_max: " << int_max << '\n' ; // 2147483647 on this implementation

    int test ;

    std::cin >> test ; // enter value greater than int_max (say, 2147483648 on this implementation)
    // the value entered is too large to be represented by an int
    // the largest representable positive value for an int is stored in test
    // and failbit is set on the input stream
    std::cout << "test: " << test << '\n' ; // int_max (2147483647 on this implementation)
    std::cout << std::boolalpha << "std::cin::fail()? " << std::cin.fail() << '\n' ; // true

    std::cin.clear() ; // clear the failed state of std::cin

    std::cin >> test ; // enter value smaller than int_min (say -2147483699)
    // the negative value entered is outside the range that can be represented by an int
    // the lowest representable negative value for an int is stored in test
    // and failbit is set on the input stream
    std::cout << "test: " << test << '\n' ; // int_min (-2147483648 on this implementation)

    test = int_max ;
    ++test ; // engenders undefined behaviour
}

http://coliru.stacked-crooked.com/a/36480eda30758774
Thanks NT3 & Chervil,

What are signed and unsigned integer and signed and unsigned character?why and where it is used?

Last edited on
Topic archived. No new replies allowed.