I what to know that what is the base on which the size of any datatype is decided. For eg. size of intger datatype in java is 4 bytes. So why its exactly 4 and not anything else. More over maximum number that can be stored with int type is 2,147,483,648. When this number come from? I mean what is the formula to get this number for anyother data type?
But why it is exactly 4? I still not clear. Some says that to represent any number in the range of perticular datatype. But there are other dataypes also which provide larger range. Still so much confused…
In assembler size in bits of the types is defined by the architecture that has to work with the numbers. Most current architectures group bits in 8-bit bytes, and double the size of the number when going to the next type: 8, 16, 32, 64, 128… but not all architectures support all the types, and some old architectures had weird types (14 bit integers, for example).
When you use a programming language, the language abstracts the lower levels, and the types in the language are defined to abstract the lower level types. Depending on the language the types might differ:
char,short int,intsome proposedshort,tall,grande. In some cases it will be exactly defined (in Java or C# anintis exactly 32bits, and alongis 64bits), while in others like C/C++ only the relationships among the types are defined (longis not smaller thanintwhich in turn is not smaller thanshort, thenchar…)The maximum number that a given unsigned type can hold can be calculated as
2^N-1, so for a 32 bit unsigned int, the maximum value is4294967295. The reason for the-1part is that there are2^Ndistinct numbers, one of which is0, so only2^N-1are left for non-zero values.When it comes to signed integer types, again the architecture has much to say. Most current architectures use two’s-complement. In those platforms the highest signed value for an
Nbit signed integer will be2^(N-1)-1and the most negative number will be-2^(N-1). Some older architectures reserved one bit for the sign and then stored the actual number, in which case the negative range would be reduced by one (this approach allows for two zero values:0and-0).As to your question, you will have to pick the language first. Then, in the case of C or C++, you will have to pick your platform, and even the compiler. While in most cases the types are directly related (
intis 32 bits in most 32 and 64bit architectures in C++), that is not guaranteed, and some types might differ (longmight represent a 32 or 64bit integer type). As of the actual size of a type in C++ besidescharthat is guaranteed to haveCHAR_BITSbits, you can usesizeof(type)*CHAR_BITSto get the size in bits.