1 / 31

Sizes of simple data types

Sizes of simple data types. sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8. In one machine. sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long) = 4. In another machine. The following statements are true for all machines.

hriehle
Download Presentation

Sizes of simple data types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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.

  3. 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

  4. Bitwise operators • << Left shift • >> Right shift • & Bitwise AND • | Bitwise OR • ^ Bitwise EXCLUSIVE OR(XOR) • ` Complement (invert all bits)

  5. Operators The main operators are as follows:

  6. AND operator Bitwise operator AND returns a 1 if both operand bits are 1. 1 & 1 = 1 0 & 1 = 0 1 & 0 = 0 0 & 0 = 0 Example: 00001010 00011010 ------------------- 00001010

  7. OR operator Bitwise operator OR returns a 1 if at least one operand is a 1. 1 | 1 = 1 0 | 1 = 1 1 | 0 = 1 0 | 0 = 0 Example: 00101001 10100011 ------------- 10101011

  8. XOR Bitwise XOR (exclusive OR) returns a 1 if only one of the operands is a 1. 1 ^ 1 = 0 0 ^ 1 = 1 1 ^ 0 = 1 0 ^ 0 = 0 Example: 10001011 11000111 ------------- 01001100

  9. 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.                                 

  10. 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.

  11. 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.

  12. 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

  13. type int format type double format binary number mantissa exponent 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.

  14. 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

  15. 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)

  16. 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

  17. 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.

  18. 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.

  19. 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

  20. 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)

  21. 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.

  22. 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.

  23. 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; • 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. • 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. Expression value = 1 z = 1.0

  24. Automatic Conversion of Data Types • Example: • int k = 5, m = 4, n; • double x = 1.5, y = 2.1, z; • 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

  25. 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

  26. Explicit Conversion of Data Types Difference • A. frac = (double)(n1 / n2); and • B. frac = (double)n1 / (double)n2; • C. frac = (double)n1 /n2; • The quotient n1/n2 is computed first, results in loss of fractional part. • The cast to double convertsthe whole number quotient tp type dpouble. • frac = (double)n1 / (double)n2; • 2.0 / 4.0 • 0.5 • frac = (double)n1 / n2; n2 will automatically be converted to double.

  27. 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.

  28. 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.

  29. 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

  30. 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); • }

  31. 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”; • } • }

More Related