answersLogoWhite

0


Best Answer

To determine the range of an integer data type you first need to know the size of the integer, in bytes. This is implementation-dependant, so never assume it will always be 4 bytes. On a 64-bit system, it could be 4 or 8.

To accurately determine the size of any datatype, always use the sizeof() operator. If you don't, then you are guaranteed to run into major problems when porting programs between compilers and architectures.

Once you have the size, determining the range is fairly easy. The following program contains two function overloads to determine the range of an int and an unsigned int, using bitwise shift operations. The main function tests both functions, showing the ranges in both decimal and hexadecimal (example output is shown below).

#include <iostream>

#include <iomanip>

size_t GetRange(const int& i, int& iMin, int& iMax )

{

int sizeInBits = sizeof(i) * 8;

iMin = 1;

iMin <<= --sizeInBits;

iMax = 1;

while(--sizeInBits)

{

iMax <<= 1;

iMax |= 1;

}

return(sizeof(i));

}

size_t GetRange(const unsigned int& u, unsigned int& uMin, unsigned int& uMax )

{

unsigned int sizeInBits = sizeof(u) * 8;

uMin = 0;

uMax = 1;

while(--sizeInBits)

{

uMax <<= 1;

uMax |= 1;

}

return(sizeof(u));

}

int main()

{

using namespace std;

const int w=17, z=12;

int x, iMin, iMax;

unsigned int y, uMin, uMax;

size_t iSize=GetRange(x, iMin, iMax);

size_t uSize=GetRange(y, uMin, uMax);

cout<<setw(z)<<"Type"<<setw(w)<<"int (x)"<<setw(w)<<"unsigned int (y)"<<endl;

cout<<setw(z)<<"Size (bytes)"<<setw(w)<<iSize<<setw(w)<<uSize<<endl;

cout<<setbase(10);

cout<<setw(z)<<"Min (dec)"<<setw(w)<<iMin<<setw(w)<<uMin<<endl;

cout<<setw(z)<<"Max (dec)"<<setw(w)<<iMax<<setw(w)<<uMax<<endl;

cout<<setbase(16);

cout<<setw(z)<<"Min (hex)"<<setw(w)<<iMin<<setw(w)<<uMin<<endl;

cout<<setw(z)<<"Max (hex)"<<setw(w)<<iMax<<setw(w)<<uMax<<endl;

cout<<endl<<endl;

return(0);

}

Output:

Typeint (x)unsigned int (y)Size (bytes)44Min (dec)-21474836480Max (dec)21474836474294967295Min (hex)800000000Max (hex)7fffffffffffffff

Note the way signed integers are represented in hexadecimal. It looks odd but the binary representation of -1 in 8-bit notation is 11111111 (FFh), not 10000001 (81h) as you might expect. It makes more sense when you realise 11111111+00000001=00000000 which, in decimal, would be -1+1=0.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

values can be calculated using the general formula which is given below,

no of values data type can have=2^n(2 power n),where n represents no of bits.

so the value of byte data type=2^8(i.e 1 byte=8 bits),here n=8

byte value=256

This answer is:
User Avatar

User Avatar

Wiki User

11y ago
  • In a 16-bit system, MSB bit is considered as a sign bit. We skip that bit.
  • So 16 bits - 1 bits = 15 bits
  • Then an Integer value is 2 bytes.
  • Max range = 2 raise to 15 ( 215 )

= 32767

  • Min range = 2 raise to -15 ( 2-15 )

= -32767

Helen

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

To calculate this, you would need information about how the information is stored internally in this data type - how many bits for the mantissa, how many for the exponent - and how the bits are interpreted (since there may be an offset).

<><><><><>

You could also include the standard include file FLOAT.H and query the various limits, such as FLT_MAX and FLT_MIN.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you calculate integer data type?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What data type is age?

integer data type


What is the data type int?

Data-type (short for integer).


What is data and define its type?

Data is information. Data type defines the type of data - integer, character etc


What is the default data type?

integer


What is java's keyword for the integer data type?

"int" is the keyword for integer


How much memory is required to store an integer data type?

integer data type consumes memory of 4 bytes or 32 bits


What type of data is acceptable for arrays index?

The details depend on the language, but the index of an array is usually an integer data type. Anything that is compatible with an integer can be used.


Specific example of integer data type?

1


What does Data types?

Data type means it tells the compiler the variable belongs to integer .character.floating.l


Abstract data type that store a whole numbers?

All data types can be used to store a whole number, even the data types that can store a decimal number.


What typeof data type would be best to store an age?

integer


What is a valid variable data type?

1. If its natural or integer numbers- Integer(Int) data type. 2. If it consists of decimal or fraction part- Double or float data type. 3. If it has a single letter or sign- Character(Char) data type. 4. If its got many words(alpha-numerical)- String data type. 5. If the result has to be "true" or "false"- Boolean data type.