31
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long) = 4 In one machine In another machine

Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

  • View
    224

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Sizes of simple data types

sizeof(char) = 1size(short) = 2sizeof(int) = 4size(long) = 8

sizeof(char) = 1size(short) = 2sizeof(int) = 2size(long) = 4

In one machine

In another machine

Page 2: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

The following statements are true for all machines

• 1 = sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

• 1 <= sizeof(bool) <= sizeof(long)

• sizeof(float) <= sizeof(double) <= sizeof(long double)

• A char is at least 8 bits

• A short is at least 16 bits.

• A long is at least 32 bits.

Page 3: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Different systems

• Binary system: 0 or 1 => two digits

• Decimal system: 0,1,2,3,4,5,6,7,8,9=>ten digits.

• Octal system: 0,1,2,3,4,5,6,7 => 8 digits

• Hexadecimal system:

0,1,2,3,4,5,6,7,8,9,A, B, C, D, E, and F

=> 16 digits

Page 4: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Bitwise operators

• << Left shift

• >> Right shift

• & Bitwise AND

• | Bitwise OR

• ^ Bitwise EXCLUSIVE OR(XOR)

• ` Complement (invert all bits)

Page 5: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Operators

The main operators are as follows:

                                 

Page 6: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

AND operatorBitwise operator AND returns a 1 if both operand bits are 1.

1 & 1 = 10 & 1 = 01 & 0 = 00 & 0 = 0

Example: 00001010

00011010------------------- 00001010

Page 7: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

OR operatorBitwise operator OR returns a 1 if at least one operand is a 1.

1 | 1 = 10 | 1 = 11 | 0 = 10 | 0 = 0

Example: 00101001

10100011-------------10101011

Page 8: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

XORBitwise XOR (exclusive OR) returns a 1 if only one of the operands is a 1.

1 ^ 1 = 00 ^ 1 = 11 ^ 0 = 10 ^ 0 = 0Example:

1000101111000111-------------01001100

Page 9: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Left Bit Shift

Left Bit Shift moves the bit values left to a specified position. Bits to the left are moved out of focus and the new bits on the right are set to zero.                                 

Page 10: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Right Bit Shift

Right Bit Shift moves the bit values right to a specified position. Bits to the right are moved out of focus and the new bits on the left are set to zero.

Page 11: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Representation and conversion of Numeric Types

• Why we need more than one numeric type ?• We could use data type double for all numbers.

However, they are represented in the computer’s memory in different ways. Here are some advantages of integer data type:– On many computers, operation will be faster if we use

integer instead of type double.

– We need less space to store integer number .

– Operations with integer are always precise. But in operations with double numbers can result in some loss of accuracy or round-off error.

Page 12: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Advantages of Type double Format

• A real number has an integral part and a fractional part. But an integer number can not have a fractional part. So real number can not be represented by an integer, it can be represented by double data type.

• Another advantage of the data type double is that much larger range of numbers can be represented as compared to type int.

• Actual ranges vary from one implementation to another.

ANSI standard for C

min range of positive values of type int: 1- 32, 767

min range of positive values of type double: 10 -37 to 10 37

Page 13: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Internal formats of Type int & Type double• All data are represented in memory as binary strings (strings

of 0s and 1s).

• However, the binary string stored for type int value 100 is not the same as the binary string stored for the type double number 100.0.

• The actual internal representation is computer dependent.

• Positive integers are represented by standard binary numbers. Integer 100 is represented by binary number 01100100.

• The format of type double is analogous to scientific notation.

mantissa exponent

type double format

binary number

type int format

Page 14: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Scientific Notation A real number has an integral part and a fractional part

which are separated by a decimal point.

• Very large or very small values can be represented by scientific notation.

Example: 123000.0

• In C++ scientific notation: 1.23e5 or 1.23E5

• Read the letter e or E as "times 10 to the power".

• If the exponent has a minus sign, the decimal point is moved to the left.

Example: 0.34e-4 == 0.000034

Page 15: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Floating-point notation• Usual method to represent real numbers• Real number is represented by a number, called a

mantissa, times a base raised to an integer power, called an exponent.

• Example:

387.53 = 38753 x 10 -2

Other possibilities .38753 x 10 3, 387.53 x100

(we choose mantissa is an integer with no tailing 0s)

Page 16: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Floating-point notation(cont.)• A real number is represented by a 32-bit string.• 24-bit for mantissa and 8-bit for exponent.• Base is fixed to 10.• Examples

100 = 0000000000000000000000100000010

-387.53 = 11111111011010001001111111111110

387.53 = 00000000100101110110000111111110

24-bit binary representation of 38753 is 000000001001011101100001

8-bit twos complement binary representation of -2 is 11111110

Page 17: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Representation & Conversion of Type char• We can declare variable of type char to represent a single

character.• A character can be a letter, digit, or punctuation mark.• A character is enclosed in apostrophes.(e.g. ‘A’, ‘3’).• We can compare character values using equality operators (== and

!=) and relational operators (<,<=, >, >=).• In ASCII (American Standard Code for Information Interchange): digit character ‘0’ = 48 (decimal code value) ‘9’ = 57 (‘0’<‘1’<‘2’<‘3’<‘4’…….<‘9’) ‘A’ = 65 ‘Z’ = 90 (‘A’< ‘B’<‘C’……..<‘X’<‘Y’<‘Z’) ‘a’ = 97 ‘z’ = 122 (‘a’< ‘b’<‘c’……..<‘x’<‘y’<‘z’)• C++ allows conversion of type char to type int and vice versa.

Page 18: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Converting digit characters to integers

‘0’ – ‘0’ = 0‘1’ – ‘0’ = 1‘2’ – ‘0’ = 2‘3 – ‘0’ = 3

In ASCII, ‘0’ has internal representation 48 and ‘2’ has internal representation 50.

Page 19: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Numerical Inaccuracies• Sometimes an error occurs in representing real numbers.• Analogy: in decimal system, 1/3 is 0.333333…..• Some fractions canot be represented exactly as binary

numbers in the mantissa of the type double format.• This inaccuracy is called representational error.• It depends on the number of binary digits used in the

mantissa.• The more bits, the smaller the error. • An equality comparison of two type double values can lead

to unexpected results. Example for (count = 0.0; count != 10.0; count = count + 0.1) { }

Infinite loop

Page 20: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Cancellation Error

• When you add a very large and a very small real numbers, the larger number may cancel out the smaller number.

• Results in cancellation error.

Example:

1000.0 + 0.000000231 = 1000.0

(On some computers)

Page 21: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Arithmetic Underflow

• If we multiply two very small numbers, the result may be too small to be represented accurately.

• The computational result will be represented by zero.

• This phenomenon is called arithmetic underflow.

Page 22: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Arithmetic Overflow

• If we multiply two very large numbers, the result may be too large to be represented.

• This phenomenon is called arithmetic overflow.

• Different C compilers handle it in different ways.

Page 23: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Automatic Conversion of Data Types• In several cases, data of one numeric type are automatically

converted to another numeric type.

• Some examples:

int k = 5, m = 4, n;

double x = 1.5, y = 2.1, z;

A Expression of different numeric types:

k + x Value of int variable k is converted

Value is 6.5 to type double format before operation is performed.

B RHS is of type int and LHS is of type double:

z = k / m; Expression is evaluated first. Then, the result is converted to type

double format for assignment.z = 1.0

Expression value = 1

Page 24: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Automatic Conversion of Data Types

• Example:

int k = 5, m = 4, n;

double x = 1.5, y = 2.1, z;

C RHS is of type double and LHS is of type int:

n = x * y; Expression is evaluated first. Then, the result is converted to type int format for assignment, and

fractional part is lost.Expression value= 3.15;

n = 3

Page 25: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Explicit Conversion of Data Types• C also provides an explicit type conversion operation

called a cast.

• Examples:– Using cast operation int n1, n2;

double frac;

frac = (double)n1 / (double)n2;

2.0 / 4.0

0.5

– Without cast operation frac = n1 / n2;

2 / 4 integer division

0

Result:frac = 0.5

Result:frac = 0.0

Page 26: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Explicit Conversion of Data Types A. frac = (double)(n1 / n2); and B. frac = (double)n1 / (double)n2; C. frac = (double)n1 /n2;A The quotient n1/n2 is computed first, results in

loss of fractional part. The cast to double convertsthe whole number

quotient tp type dpouble.B frac = (double)n1 / (double)n2; 2.0 / 4.0 0.5C frac = (double)n1 / n2; n2 will automatically be

converted to double.

Difference

Page 27: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Explicit Conversion of Data Types• Sometimes we include casts that do not affect the result

but simply make clear to the reader the conversions that would occur automatically.

Example: int x, sqrt_x; sqrt_x = (int)sqrt((double)x);• The formal parameter of sqrt function is of type double.• The actual argument x is of type int.• x will automatically be converted to type double.• sqrt function returns a value of type double.• This value will automatically be converted to type int since

sqrt_x is of type int. Note: When a cast operation is applied to a variable, the

conversion changes the value of the expression, but it does not change what is stored in the variable.

Page 28: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Enumerated Types• ANSI C allows us to associate a numeric code with each

category by creating an enumerated type that has its own list of meaningful values.

• typedef can be used to name user-defined types. Example: typedef enum { sunday, monday, tuesday, wednesday, thursday, friday, saturday} day_t; enumerated type day_t has seven possible values (enumeration

constants). sunday will be represented as the integer 0, monday as integer 1,

and so on. day_t today; // declaration of variable today.

Page 29: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Examples of Enumerated Types• class_t is an enumerated data type: typedef enum {fresh, soph, jr, sr} class_t;• What is the value of each of the following? (int) sr , (class_t) 0, (class_t)((int)soph + 1)

• What is displayed by this code fragment? for (class = fresh; class <= sr; ++class) cout << class << “ “;

3 fresh jr

0 1 2 3

Page 30: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Example of Enumerated Type Example: typedef enum { sunday, monday, tuesday, wednesday, thursday, friday, saturday} day_t; day_t next(day_t today) { day_t next; if (today == saturday) next = sunday; else next = (day_t)((int)today + 1); return (next); }

Page 31: Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)

Another Example of Enumerated Type typedef enum { sunday, monday, tuesday, wednesday, thursday, friday, saturday} day_t; void print_day (day_t day) { switch (day) { case sunday: cout << “sunday”; break; case monday: cout << “monday”; break; default: cout << “Invalid Code”; }

}