Programming in C++ Example On one machine, it maybe the case that sizeof(char)=1, sizeof(short)=2 sizeof(int)=4; sizeof(long)=8 On other machine, the size might be as follows sizeof(char)=1, sizeof(short)=2 sizeof(int)=2, sizeof(long)=4
6 Example ❖On one machine,it maybe the case that sizeof(char)=1; sizeof(short)=2; sizeof(int)=4; sizeof(long)=8; ❖On other machine, the size might be as follows: sizeof(char)=1; sizeof(short)=2; sizeof(int)=2; sizeof(long)=4;
Programming in C++ The only guarantees made by C++ are 1= sizeof( char)<= sizeof(short)<= sizeof(int )<= sizeof( long) 1<=sizeof bool)<=sizeof long) sizeof float )< sizeof double )<= sizeof long double) char is at least 8 bits short is at least 16 bits long is at least 32 bits
7 The only guarantees made by C++ are . . . 1 = sizeof( char ) <= sizeof( short ) <= sizeof( int ) <= sizeof( long ) 1 <= sizeof ( bool ) <= sizeof ( long ) sizeof ( float ) <= sizeof ( double ) <= sizeof ( long double ) char is at least 8 bits short is at least 16 bits long is at least 32 bits
Programming in C++ Using one byte(=8 bits ) 01 000 HOW MANY DIFFERENT NUMBERS CAN BE REPRESENTED USING Os and 1s? Each bit can hold either a 0 or a 1. So there are just two choices for each bit. and there are 8 bits 2x2x2x2x2x2x2x2=28=256
8 Using one byte ( = 8 bits ), HOW MANY DIFFERENT NUMBERS CAN BE REPRESENTED USING 0’s and 1’s? Each bit can hold either a 0 or a 1. So there are just two choices for each bit, and there are 8 bits. 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 28 = 256 0 1 1 0 0 0 1 1
Programming in C++ Similarly, using two bytes(=16 bits) 0110001 0100 1010 16 65536 DIFFERENT NUMBERS CAN BE REPRESENTED If we wish to have only one number representing the integer zero, and half of the remaining numbers positive, and half negative, we can obtain the 65, 536 numbers in the range below -32,768 0 32767
9 Similarly, using two bytes ( = 16 bits), 2 16 = 65,536 DIFFERENT NUMBERS CAN BE REPRESENTED. If we wish to have only one number representing the integer zero, and half of the remaining numbers positive, and half negative, we can obtain the 65,536 numbers in the range below : -32,768 . . . . 0 . . . . 32,767 0 1 1 0 0 0 1 1 0 1 0 0 1 0 1 0
Programming in C++ Range of Values The interval within which values of a numeric type must fall, specified in terms of the largest and smallest allowable values 10
10 Range of Values The interval within which values of a numeric type must fall,specified in terms of the largest and smallest allowable values