CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation...

20
CP104 Introduction to Programming Simple Data Type Lecture 19__ 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

Transcript of CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation...

Page 1: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 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

Page 2: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 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 -32768 and 32767, there are total 216 = 65536 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.

Page 3: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 3

• 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, 9

321 = 321 10 = 3x10 2 + 2 x 10 1 + 1x 10 0

• Binary: base = 2, digits are 0, 1

2110 = 1x2 4 + 0x2 3 + 1x2 2 + 0x2 1 + 1x 2 0 = 101012

• Octal: base = 8, digits are 0, 1, 2, 3, 4, 5, 6, 7

2110 = 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, 15

3110 = 1F 16

Positional Representation of Numbers

Page 4: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 4

Decimal0123456789

101112131415

Examples of Number Representation 0 - 15

4 bit Binary0000000100100011010001010110011110001001101010111100110111101111

Octal01234567

1011121314151617

Hexadecimal0123456789ABCDEF

Page 5: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 5

• 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:

.111 2 = 1x2 -1 + 1 x 2 -2 + 1x 2-3 = 0.875 10

111.1112 = 1x2 2 + 1 x 2 1 + 1x 2 0 + 1x2 -1 + 1 x 2 -2 + 1x 2-3 = 7.87510

The Binary Representation of Fractions

Page 6: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 6

• Base b1 --> decimal --> base b

• Decimal to base b conversion algorithm

1. Input: an-1 an-2 … a1 a0 . c1 c2 … cn

2. Convert the integer part an-1 an-2 … a1 a0

3. quotient = an-1 an-2 … a1 a0

4. while (quotient !=0)

{ remainder = quotient % b

output the remainder

quotient = quotient / b }

5. Inverse the output sequence of the remainders.

6. Fraction part: multiply the fraction part by b repeatedly with the integer

carries forming digits from left to right.

Conversion from One Base to Another

Page 7: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 7

• Example: convert (37 . 375)10 to binary number

Integer part: 3737 / 2 --> quotient =18, remainder = 118 / 2 --> quotient =9, remainder = 09 / 2 --> quotient =4, remainder = 14 / 2 --> quotient =2, remainder = 02 / 2 --> quotient =1, remainder = 01 / 2 --> quotient = 0, remainder = 1. Quotient = 0. Then 3710 = 1001012

Fraction part: 0.3750.375 x 2 --> 0.75 carry 00.75 x 2 --> 1.5 carry 10.5 x 2 --> 1.0 carry 1, 0.375 10 = 0.0112

Answer: 37 . 37510 = 100101. 0112

Example of Conversion

Page 8: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 8

• 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: 111 111 2 --> 7 7 8

• Octal to binary:

Convert each octal digit to three digit binary

number.

Example: 668 -->1101102

Conversions Among binary, octal and hex

Page 9: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 9

• Binary to hexadecimal:

Converting each group of four binary digits to one

hexadecimal digit from right to left

Example: 11 11112 --> 3F 8

• Hexadecimal to binary:

Convert each hexadecimal digit to four digit binary numbe

• Hexadecimal to octal / octal to hexadecimal:

Hexadecimal <--> binary <--> octal

Conversions Among binary, octal and hex

Page 10: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 10

• Unsigned integers unsigned int for positive integers:

Use 16 bits, represent 0, 1, …, 65537.

• Overflow: the resulting integer is out of the range of the scheme

• Add two unsigned integers

– Example: unsigned 8 digit numbers

0000 0000 1001 1000

+ 0000 0000 0011 0011

0000 0000 11001011

– If the sum is bigger than the maximum number 1111 1111 1111

1111

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.

Unsigned Integer Data Type

Page 11: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 11

• int is a signed integer data type. (16 bits) The range of integers

represented is -2 15, …, -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 = 12 10 = 0000 0000 0000 1100 2

1 0000 0000 0000 0000

- 0000 0000 0000 1100 (this is 12 in decimal)

1111 1111 1111 0100 (this is the two’s complement of -12 )

Signed Integer Data Type

Page 12: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 12

Given two’s complement of N, how to get the two’s complement of –N

Method one: Invert all bits followed by adding 1

0000000000001100

1111111111110011

+ 1

11111111 11110100

Method two: Invert all bits from left to right before the right most 1

0000000000001100

1111111111110100

How to Get two’s Complement

Page 13: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 13

• 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:

- 12 – 12

= - 24

Addition and Subtraction of integers

1111111111110100+ 1111111111110100

1111111111101000

11 are the last two carriers. Not overflow

Page 14: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 14

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

Representation of floating point numbers

Page 15: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 15

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

Representation of floating point numbers

Page 16: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 16

• Example: 1. 100001 x 2 4

s = 0, or 1, M = 1.100001, e = 4

Mantissa [22-0]: 1000 0100 0000 0000 0000 00

Exponent [30-23]: E = 4 + 127 = 131 = 1000 0011

Sign [31] : 0

Single precision: 0 1000 0011 1000 0100 0000 0000 0000 00

Example of Single Precision Floating Point

Page 17: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 17

• Range of single precision– Negative numbers less than -(2-2-23) × 2127 (negative

overflow) – Negative numbers greater than -2-149 (negative

underflow) – Zero – Positive numbers less than 2-149 (positive underflow) – Positive numbers greater than (2-2-23) × 2127 (positive

overflow)

Range, overflow, underflow

Page 18: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 18

• 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 = 0.333333333…. => 0.333333– Effect on comparison

• Cancellation error– Example: 1000.0 + 0.0001234

Numerical Inaccuracies

Page 19: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 19

• To convert decimal 17.15 to IEEE single precision floating point:

• Convert decimal 17 to binary 10001.

• Convert decimal 0.15 to the repeating binary fraction 0.001001.

• Combine integer and fraction to obtain binary 10001.001001

• Normalize the binary number to obtain 1.0001001001x2 4

• Hence, M = 00010010011001100110011

E = 4+127 = 131 = 10000011.

• The number is positive, so S=0.

• Assign the values for M, E, and S in the correct fields, the representation is

0100 0001 1000 1001 0011 0011

0011 0011

The hexadecimal representation is 4 1 8 9 3 3 3 3

• The floating point of –17.15 is

1100 0001 1000 1001 0011 0011

0011 0011

The hexadecimal presentation is C 1 8 9 3 3 3 3

Floating Point Example

Page 20: CP104 Introduction to Programming Simple Data Type Lecture 19__ 1 Simple Data Type Representation and conversion of numbers Integer data types and representation.

CP104 Introduction to Programming Simple Data Type Lecture 19__ 20

ASCII Character Representation