Presentation is loading. Please wait.

Presentation is loading. Please wait.

Simple Data Type Representation and conversion of numbers

Similar presentations


Presentation on theme: "Simple Data Type Representation and conversion of numbers"— Presentation transcript:

1 Simple Data Type Representation and conversion of numbers
Integer data types and representation Representation of real numbers, single precision floating point Character types Enumerated types Case study

2 Data Type and Representation
A data type is a set of numbers together with a set of operations Example: integer data type int (16 bits) consists of all integers between and 32767, there are total 216 = different integers Operations: add, subtract, multiply, and division, modular All numbers have to be converted to binary format at compiling / assembling time Different data types are represented differently in binary format, so that the operations on them also work differently.

3 Positional Representation of Numbers
Given a positive integer b as a base. 0, 1, 2, …, b-1 are the digits of base b. Suppose an-1, an-2 , …, a1, a0 are a sequence of digits of base b. Then the sequence an-1 an-2 … a1 a0 ( b ) is called the positional representation in base b of integer an-1 b n-1 + an-2 b n-2 + … + a1 b 1 + a0 b 0 Decimal: base = 10, digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, = = 3x x x 10 0 Binary: base = 2, digits are 0, = 1x x x x x 2 0 = Octal: base = 8, digits are 0, 1, 2, 3, 4, 5, 6, = 25 8 Hexadecimal: base = 16, digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, D, F, where A, B, C, D, E, F stand for 10, 11, 12, 13, 14, = 1F 16

4 Examples of Number Representation 0 - 15
Decimal 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 4 bit Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Octal 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 Hexadecimal 1 2 3 4 5 6 7 8 9 A B C D E F

5 The Binary Representation of Fractions
Suppose c1, c2 , …, cn are a sequence of bits of base b. Then the sequence . c1 c2 … cn (b ) is called positional representation in base b of fraction c1 b -1 + c2 b -2 + … + cn b -n Examples: = 1x x x = = 1x x x x x x =

6 Conversion from One Base to Another
Base b1 --> decimal --> base b Decimal to base b conversion algorithm Input: an-1 an-2 … a1 a0 . c1 c2 … cn Convert the integer part an-1 an-2 … a1 a0 quotient = an-1 an-2 … a1 a0 while (quotient !=0) { remainder = quotient % b output the remainder quotient = quotient / b } Inverse the output sequence of the remainders. Fraction part: multiply the fraction part by b repeatedly with the integer carries forming digits from left to right.

7 Example of Conversion Example: convert (37 . 375)10 to binary number
Integer part: 37 37 / 2 --> quotient =18, remainder = 1 18 / 2 --> quotient =9, remainder = 0 9 / 2 --> quotient =4, remainder = 1 4 / 2 --> quotient =2, remainder = 0 2 / 2 --> quotient =1, remainder = 0 1 / 2 --> quotient = 0, remainder = 1. Quotient = 0. Then = Fraction part: 0.375 x 2 --> carry 0 0.75 x 2 --> 1.5 carry 1 0.5 x 2 --> 1.0 carry 1, = Answer: =

8 Conversions Among binary, octal and hex
Binary to octal: Convert each each group of three binary digits to one octal digit from the right to left will result in the octal number. Example: > Octal to binary: Convert each octal digit to three digit binary number. Example: >

9 Conversions Among binary, octal and hex
Binary to hexadecimal: Converting each group of four binary digits to one hexadecimal digit from right to left Example: > 3F 8 Hexadecimal to binary: Convert each hexadecimal digit to four digit binary numbe Hexadecimal to octal / octal to hexadecimal: Hexadecimal <--> binary <--> octal

10 Unsigned Integer Data Type
Unsigned integers unsigned int for positive integers: Use 16 bits, represent 0, 1, …, Overflow: the resulting integer is out of the range of the scheme Add two unsigned integers Example: unsigned 8 digit numbers If the sum is bigger than the maximum number the overflow happens, and the resulting 16 digits is not a correct answer ! Overflow if and only if the carry of the left most digits is 1.

11 Signed Integer Data Type
int is a signed integer data type. (16 bits) The range of integers represented is , …, -1, 0, 1, …, 2 15 – 1. Two’s complement representation leading bit (the left most) 0 indicates positive number, 1 negative. For an n bit pattern, use use 2 n – N to represent -N Example N = = (this is 12 in decimal) (this is the two’s complement of -12 )

12 How to Get two’s Complement
Given two’s complement of N, how to get the two’s complement of –N Method one: Invert all bits followed by adding 1 Method two: Invert all bits from left to right before the right most 1

13 Addition and Subtraction of integers
Add/subtraction in two’s complement : Do sum as usual. Do subtraction as a – b = a + (-b) Overflow if and only if the carries of the left two digits are not same Example: – = - 24 are the last two carriers. Not overflow

14 Representation of floating point numbers
The IEEE 754 standard binary representation for floating point numbers. single precision (32bits): float double precision (64bits) double 32 bits Single precision: based on scientific binary representation, (-1) s x 1.M x 2 e M is mantissa, e is exponent, s is sign, 2 is the base the normalized mantissa M is stored in bits 22-0 with a hidden 1 bit the exponent, e, is represented as an excess 127 integer in bits 30-23, the value actually stored is E = e + 127 the sign bit, s, indicates the sign of the mantissa, with s=0 for positive values and s=1 for negative values.

15 Representation of floating point numbers
The IEEE 754 standard binary representation for floating point numbers. single precision (32bits): float double precision (64bits) double 32 bits Single precision: based on scientific binary representation, (-1) s x 1.M x 2 e M is mantissa, e is exponent, s is sign, 2 is the base the normalized mantissa M is stored in bits 22-0 with a hidden 1 bit the exponent, e, is represented as an excess 127 integer in bits 30-23, the value actually stored is E = e + 127 the sign bit, s, indicates the sign of the mantissa, with s=0 for positive values and s=1 for negative values.

16 Example of Single Precision Floating Point
Example: x 2 4 s = 0, or 1, M = , e = 4 Mantissa [22-0]: Exponent [30-23]: E = = 131 = Sign [31] : 0 Single precision:

17 Range, overflow, underflow
Range of single precision Negative numbers less than -(2-2-23) × 2127 (negative overflow) Negative numbers greater than (negative underflow) Zero Positive numbers less than (positive underflow) Positive numbers greater than (2-2-23) × 2127 (positive overflow)

18 Numerical Inaccuracies
Beware of the inaccuracy with float point representation Representational error (round off error) The number of bits in mantissa is limited. 23 for single precision. Round off is done. It can only represent 6 significant digits (in decimal). Example: 1/3.0 = …. => Effect on comparison Cancellation error Example:

19 Floating Point Example
To convert decimal to IEEE single precision floating point: Convert decimal 17 to binary Convert decimal 0.15 to the repeating binary fraction Combine integer and fraction to obtain binary Normalize the binary number to obtain x2 4 Hence, M = E = = 131 = The number is positive, so S=0. Assign the values for M, E, and S in the correct fields, the representation is The hexadecimal representation is The floating point of –17.15 is The hexadecimal presentation is C

20 ASCII Character Representation


Download ppt "Simple Data Type Representation and conversion of numbers"

Similar presentations


Ads by Google