Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture Overview Introduction Positional Numbering System

Similar presentations


Presentation on theme: "Lecture Overview Introduction Positional Numbering System"— Presentation transcript:

1 Lecture 2 Data Representation in Computer Systems Lecture Duration: 2 Hours

2 Lecture Overview Introduction Positional Numbering System
Decimal to binary conversion Signed integer representation Floating-point representation

3 Some Notifications – A reminder (1/2)
Introduction Some Notifications – A reminder (1/2) Bit: The most basic unit of information in a digital computer (On/Off ; 0/1 state) Byte: A set of 8bits Word: two or more adjacent bytes that are manipulated collectively Word size: The size of a word in bits depends on the computer organization (16, 32, 64 bits, …) Nibbles (or nybbles): set of 4 bits – Usually a set of 8 bits is divided into two nibbles, a low order nibble and a high order nibble

4 Some notifications – A reminder (2/2)
Introduction Some notifications – A reminder (2/2) Example: Most Significant bit (MSB) Least Significant bit (LSB) bit bit bit bit bit bit bit bit bit bit bit bit bit bit bit bit High Order nibble Low Order nibble High Order nibble Low Order nibble byte byte Word (16 bit)

5 Lecture Overview Introduction Positional Numbering System
Decimal to binary conversion Signed integer representation Floating-point representation

6 Positional Numbering System (1/3)
Any numeric value is represented through increasing powers of a radix (or base) The set of valid numerals (digits) is equal in size to the radix of that system The least numeral is 0 and the highest one in 1 smaller than the radix Example: In the decimal system (base 10) The radix is 10 The number of valid numerals is 10 (equal to the radix) The set of valid numerals is: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

7 Positional Numbering System (2/3)
The most important radices (bases) in computer science are: Binary Radix 2 or base 2 Numerals: {0 , 1} Octal Radix 8 or Base 8 Numerals: {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7} Hexadecimal Radix 16 or base 16 Numerals: {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , A , B , C , D , E , F}

8 Positional Numbering System (3/3)
Any numeric value is represented through increasing powers of a radix (or base) Examples = 2x x x x x10-2 2123 = 2x32 + 1x31 + 2x30 = 2310 = 1x24 + 0x23 + 1x22 + 1x21 + 0x20 + 0x x2-2=

9 Lecture Overview Introduction Positional Numbering System
Decimal to binary conversion Converting Unsigned Whole Numbers Converting fractions Converting between Power-of-Two Radices Signed integer representation Floating-point representation

10 Some numbers to remember (1/1)
Decimal to binary conversion Some numbers to remember (1/1) Keep in mind the following tables or how to obtain them!

11 Converting Unsigned Whole Numbers (1/6)
Decimal to binary conversion Converting Unsigned Whole Numbers (1/6) A real number can take any value (ex ; ) Whole number: No fractions (ex: 10, 1231, 3543, …, -12, ,…) Unsigned number: Only positive numbers (ex: , , 12357, …) Unsigned whole numbers: No fraction and only positive numbers

12 Converting Unsigned Whole Numbers (2/6)
Decimal to binary conversion Converting Unsigned Whole Numbers (2/6) Convert the decimal number to binary: = Method 1: Repeated subtraction 113 - 64 49 - 32 17 - 16 1 - 1 1 1 11310 = 1 1

13 Converting Unsigned Whole Numbers (3/6)
Decimal to binary conversion Converting Unsigned Whole Numbers (3/6) Method 2: Division-remainder 2 |113 2 |56 2 |28 2 |14 2 |7 2 |3 2 |1 Remainder 1 Remainder 0 LSB 11310 = MSB

14 Converting Unsigned Whole Numbers (4/6)
Decimal to binary conversion Converting Unsigned Whole Numbers (4/6) A binary number with N bits can represent 2N unsigned integers from 0 to 2N-1 Example: Having N=4 bits, we can represent 24 = 16 unsigned integers from 0 to 24-1=16-1=15 The number 16 CANNOT be represented with only 4 bits!!

15 Converting Unsigned Whole Numbers (5/6)
Decimal to binary conversion Converting Unsigned Whole Numbers (5/6) The subtraction method is cumbersome. The subtraction method requires a familiarity with the powers of the radix being used. The division-remainder method is faster and easier than the repeated subtraction method. The division-remainder method can be used to convert from decimal to any other base system (not only to base 2).

16 Converting Unsigned Whole Numbers (6/6)
Decimal to binary conversion Converting Unsigned Whole Numbers (6/6) Example: Convert to base 3 using the division-remainder method. 3 |104 3 |34 3 |11 3 |3 3 |1 Remainder 2 Remainder 1 Remainder 0 10410 =

17 Lecture Overview Introduction Positional Numbering System
Decimal to binary conversion Converting Unsigned Whole Numbers Converting fractions Converting between Power-of-Two Radices Signed integer representation Floating-point representation

18 Converting fractions (1/5)
Decimal to binary conversion Converting fractions (1/5) Fractions in a decimal system can be converted/approximated to fractions in any other radix system Radix points separate the integer part of a number from its fractional part Example of fractions (the integer part is italic and the fractional part is bold) Base 10 : Base 3 : Base 2 : The “radix point” is called a “decimal point” in a decimal system, a “binary point” in a binary system, and so on…

19 Converting fractions (2/5)
Decimal to binary conversion Converting fractions (2/5) To convert fractions from decimal to any other base system we repeatedly multiply by the destination radix Example: Convert to base 5. 0.4304 x The integer part is 2 0.1520 The integer part is 0 The integer part is 3 0.8000 The integer part is 4, the fractional part is zero, we are done =

20 Converting fractions (3/5)
Decimal to binary conversion Converting fractions (3/5) Some fractions in one base could be indeterminate Fractions that contain repeating strings of digits to the right of the radix point Example: (2/3)10=(0.666…)10 An indeterminate fraction in one base could be determinate in another base (and vice-versa). Example: (2/3)10=0.23=(0.666…)10 2/3 is indeterminate in base 10 but determinate in base 3. When a fraction is indeterminate, an approximation is needed We fix the number of digits to the right of the radix point Also, approximation is needed due to the limited computing resources (example: limited size of the processor’s registers)

21 Converting fractions (4/5)
Decimal to binary conversion Converting fractions (4/5) Example: Convert to binary with 4 bits to the right of the binary point. x 2 This is our fourth bit. We will stop here. =

22 Converting fractions (5/5)
Decimal to binary conversion Converting fractions (5/5) Convert to binary: = By using the methods just described we will have: 2610= and = So =

23 Going back to positional numbering system (1/1)
Decimal to binary conversion Going back to positional numbering system (1/1) Any unsigned whole or fractional number could be converted to decimal by using the “Positional Numbering System” described previously Examples: =0x2-1+1x2-2+0x2-3+1x2-4 = = = 1x52 + 3x51 + 4x50 + 2x x x x5-4 =

24 Lecture Overview Introduction Positional Numbering System
Decimal to binary conversion Converting Unsigned Whole Numbers Converting fractions Converting between Power-of-Two Radices Signed integer representation Floating-point representation

25 Converting between Power-of-Two Radices (1/4)
Decimal to binary conversion Converting between Power-of-Two Radices (1/4) To convert between any base to any other base (different than base 10), it is easier to pass through base 10. Example: 31214= ? First step: = 3x43 + 1x42 + 2x41 + 1x40=21710 Second step: by using the division-remainder method: = So 31214=220013 Working between bases that are powers of two is much more easier.

26 Converting between Power-of-Two Radices (2/4)
Decimal to binary conversion Converting between Power-of-Two Radices (2/4) The must famous power-of-two radices are: binary (base 2), octal (base 23 / base 8) and hexadecimal (base 24 / base 16). Each octal digit is equivalent to a group of 3 binary digits called octet1 Each hexadecimal digit is equivalent to a group of 4 binary digits called hextet We convert from binary to octal and from binary to hexadecimal by simply grouping bits 1 The term “Octet” could also be used in the literature to describe a set of 8 bits.

27 Converting between Power-of-Two Radices (3/4)
Decimal to binary conversion Converting between Power-of-Two Radices (3/4) Example: Convert to octal Make Groups of 3 bits (from right to left): Add zero(s) on the left to complete the last octet Convert each octet to its corresponding octal digit Finally: =

28 Converting between Power-of-Two Radices (4/4)
Decimal to binary conversion Converting between Power-of-Two Radices (4/4) Example: Convert to hexadecimal Make Groups of 4 bits (from right to left): Add zero(s) on the left to complete the last hextet Convert each hextet to its corresponding hexadecimal digit C D Finally: = 2C9D16

29 Lecture Overview Introduction Positional Numbering System
Decimal to binary conversion Signed integer representation Signed Magnitude Complement system Floating-point representation

30 Signed integer representation
An integer is a whole number Signed integers are the set of positive and negative whole numbers How should we encode and deal with the actual sign of the number? Two concepts are used Signed Magnitude concept Complement concept

31 Signed magnitude is the most intuitive method
Signed integer representation Signed Magnitude (1/13) Signed magnitude is the most intuitive method The MSB (Most Significant Bit) of a binary number is kept as the “sign” of the number MSB = 1: negative number MSB = 0: positive number The remaining bits represent the magnitude (or absolute value) of the numeric value

32 Signed integer representation
Signed Magnitude (2/13) Example: In a 8 bit word signed magnitude system give the decimal representation of the following numbers ? The MSB is 0: The number is positive The remaining 7 bits are: = 110 The decimal number is +1 ? The MSB is 1: The number is negative The decimal number is -1

33 Signed integer representation
Signed Magnitude (3/13) Example: In a 8 bit word signed magnitude system give the decimal representation of the following numbers ? The MSB is 1: The number is negative The remaining 7 bits are: = 910 The decimal number is -9 ? The MSB is 0: The number is positive The remaining 7 bits are: = 6510 The decimal number is +65

34 In a N bit word signed magnitude system
Signed integer representation Signed Magnitude (4/13) In a N bit word signed magnitude system 1 bit is used for the sign of the number N-1 bits are used for the magnitude of the number The largest integer is 2N-1 - 1 The smallest integer is -(2N-1 - 1) Example: in a 8 bit word signed magnitude system The largest integer is = 27-1 = 12710 The smallest integer is = -(27-1) =

35 Computers should be able to carry out mathematical operations
Signed integer representation Signed Magnitude (5/13) Computers should be able to carry out mathematical operations Signed-magnitude arithmetic is carried out using essentially the same methods as humans At first we look at the signs of the two operands We arrange the operands in a certain way based on their signs We perform the calculation without regard to the signs Finally, we supply the sign as appropriate

36 Adding operands that have the same sign
Signed integer representation Signed Magnitude (6/13) Adding operands that have the same sign Example: Add to using signed-magnitude arithmetic. ⇐ carries (79) (35) (114) We find = in signed-magnitude representation. Sign

37 Example: Add 010000012 to 011000012 using signed-magnitude arithmetic
Signed integer representation Signed Magnitude (7/13) Overflow condition In the last example, adding the seventh’ bits to the left gives no carry If there is a carry, we say that we have an overflow condition and the carry is discarded, resulting in an incorrect sum. Example: Add to using signed-magnitude arithmetic

38 The addition overflows The last carry is discarded
Signed integer representation Signed Magnitude (8/13) ⇐ carries (65) (97) The addition overflows The last carry is discarded The sum’s result is incorrect X (34)

39 Signed integer representation
Signed Magnitude (9/13) Signed-magnitude subtraction is carried out in a manner similar to pencil and paper decimal arithmetic Example 1: Subtract (79) from (99) using signed-magnitude arithmetic. ⇐ borrows (99) (79) (20) We find = in signed-magnitude representation.

40 Signed integer representation
Signed Magnitude (10/13) Example 2: Subtract (99) from (79) using signed-magnitude arithmetic. Here the subtrahend, , is larger than the minuend, With the result obtained in Example 2.12, we know that the difference of these two numbers is Because the subtrahend is larger than the minuend, all that we need to do is change the sign of the difference. So we find = in signed-magnitude representation

41 Signed integer representation
Signed Magnitude (11/13) Example 3: Add (-19) to (+13) using signed-magnitude arithmetic. The result is negative We subtract 13 from 19 The result of the binary subtraction is: (-6) Example 4: Subtract (-24) from (-43) using signed-magnitude arithmetic. This is equivalent to adding -43 to 24 We subtract 24 from 43 The result of the binary subtraction is: (-19)

42 General rules when operands have different signs
Signed integer representation Signed Magnitude (12/13) General rules when operands have different signs Determine which operand has the larger magnitude The sign of the result is the same as the sign of the operand with the larger magnitude the magnitude must be obtained by subtracting (not adding) the smaller one from the larger one

43 Problems related to signed magnitude
Signed integer representation Signed Magnitude (13/13) Problems related to signed magnitude To much decisions to make (larger number? ; borrows? ; what signs?). The number 0 could have two representations : and Complicated method Expensive circuits

44 Lecture Overview Introduction Positional Numbering System
Decimal to binary conversion Signed integer representation Signed Magnitude Complement system Floating-point representation

45 Complement system is used to represent/convert negative numbers only
Signed integer representation Complement system (1/19) Complement system is used to represent/convert negative numbers only When using complement system the subtraction is converted to an addition Advantages of complement system Simplify computer arithmetic No need to process sign bits separately The sign of a number is easily checked by looking at its high-order bit (MSB).

46 In base 10, “Casting out 9s” was used to subtract numbers
Signed integer representation Complement system (2/19) In base 10, “Casting out 9s” was used to subtract numbers Let’s say we wanted to find At first, is calculated 999 – 52 = 947 947 is then added to 167 and the last carry is added to the sum: 167 – 52 = = = 115 a Carries: 1 6 7 + 9 4

47 The last method uses a “diminished radix complement”
Signed integer representation Complement system (3/19) The last method uses a “diminished radix complement” Working in base r (radix), the diminished radix is given by : r-1 Example: Base 10 ; r=10 The diminished radix is r-1 = = 9 We say that a negative number is converted to its 9’s complement For example, is converted to its nine’s complement as follows: = = 7531C9

48 In a binary system r=2 The diminished radix complement is r-1 = 1
Signed integer representation Complement system (4/19) In a binary system r=2 The diminished radix complement is r-1 = 1 We say that we work in one’s complement (C1) To convert a negative number to its one’s complement this number is subtracted from all ones A positive number is directly converted to its binary representation Example: The one’s complement of is = 1010C1 It is nothing more than switching all of the 1s with 0s and vice versa!!

49 Example: Express 2310 and -910 in 8-bit binary one’s complement form.
Signed integer representation Complement system (5/19) Example: Express 2310 and -910 in 8-bit binary one’s complement form. 2310 = + ( ) = C1 -910 = - ( ) = C1

50 In one’s compliment the subtraction is converted into addition
Signed integer representation Complement system (6/19) In one’s compliment the subtraction is converted into addition Example: 2310 – 910 = (-910) Example: Add 2310 to -910 using 8-bit binary one’s complement arithmetic. The result is C1 = +( ) = 1410 Carries: 1 2310 + + (-910) 1410

51 Signed integer representation
Complement system (7/19) Example: Add 910 to using 8-bit binary one’s complement arithmetic. -2310 = - ( )2 = C1 910 = + ( ) = C1 910 + (-2310) = C C1 Result: C1 = -( ) = -1410 Carries: 1 910 + + (-2310) -1410

52 Computer engineers long ago stopped using one’s complement
Signed integer representation Complement system (8/19) In One’s complement, we still have two representations for zero: and Computer engineers long ago stopped using one’s complement A more efficient representation for binary numbers is the two’s complement

53 Two’s complement is an example of a radix complement
Signed integer representation Complement system (9/19) Two’s complement is an example of a radix complement No need to subtract one from the radix r when working in a radix complement. Example: Base 10 ; r=10 We say that a negative number is converted to its 10’s complement For example, is converted to its ten’s complement as follows: = = 7532C10

54 In a binary system r=2 The diminished radix r = 2
Signed integer representation Complement system (10/19) In a binary system r=2 The diminished radix r = 2 We say that we work in two’s complement Consider “d” is the number of digits To convert a negative number “N” to its two’s complement this number is subtracted from rd = 2d : N10 = (2d – N)C2 A positive number is directly converted to its binary representation

55 Example: In a 4 bits system: d=4;
Signed integer representation Complement system (11/19) Example: In a 4 bits system: d=4; All negative numbers are converted by being subtracted from 2d = 24 = 1610 = The two’s complement of is = 1101C2 It is nothing more than one’s complement incremented by 1!!

56 Signed integer representation
Complement system (12/19) Example: Express 2310, -2310, and -910 in 8-bit binary two’s complement form. 2310 = + ( ) = -2310 = -( ) = = -910 = -( ) = =

57 Unlike C1 arithmetic, in C2 the last carry is discarded
Signed integer representation Complement system (13/19) Unlike C1 arithmetic, in C2 the last carry is discarded Example 1: Add 910 to using two’s complement arithmetic. The result is C2 = -( ) = -1410 Carries: 1 910 + + (-2310) -1410

58 Note how a negative binary number in C2 is converted to decimal
Signed integer representation Complement system (14/19) Note how a negative binary number in C2 is converted to decimal At first all 0 and 1 in the C2’s number are switched: → A “1” is then added to the last number: = So C2 = -( ) = -1410

59 Signed integer representation
Complement system (15/19) Example 2: Find the sum of 2310 and -910 in binary using two’s complement arithmetic. 2310 = +( )2 = C2 -910 = -( ) = C2 (-910) = C C2 Result: C2 = +( ) = 1410 Carries: 1 2310 + + (-910) -1410

60 Advantages of two’s complement
Signed integer representation Complement system (16/19) Advantages of two’s complement It is the most popular choice for representing signed numbers The algorithm for adding and subtracting is quite easy It has the best representation for 0 (all 0 bits) It is self-inverting It is easily extended to larger numbers of bits.

61 Signed integer representation
Complement system (17/19) Drawback the asymmetry seen in the range of values that can be represented by N bits. Examples: With signed-magnitude, 4 bits allow us to represent the values -7 (11112) through +7 (01112). Using two’s complement, we can represent the values: -8 (1000C2) through +7 (0111C2)

62 Overflow in complement systems (C1 and C2)
Signed integer representation Complement system (18/19) Overflow in complement systems (C1 and C2) An overflow occurs if two positive numbers are added and the result is negative or if two negative numbers are added and the result is positive. It is not possible to have overflow when if a positive and a negative number are being added together.

63 Note that the last two carries are different
Signed integer representation Complement system (19/19) To Detect Overflow Check the last two carries If these are different: there is an overflow If these are equal: there is no overflow Example 1: Find the sum of and 810 in binary using two’s complement arithmetic. The result is C2 = -( )2 = !!! Note that the last two carries are different Carries: 1 12610 + -1410

64 Lecture Overview Introduction Positional Numbering System
Decimal to binary conversion Signed integer representation Floating-point representation A simple model Floating-point arithmetic Floating point errors

65 Floating-point representation (1/1)
A computer is supposed to solve all problems Huge and fractional numbers and complicated mathematical operations could be involved An optimized solution to give a good ratio: “Biggest Number/word size” is the Floating point representation

66 Numbers written in scientific notation have three components:
Computers use a form of scientific notation for floating-point representation Numbers written in scientific notation have three components: Scientific notation in base 10: Scientific notation in base 2: 0.579 x 107 + x 23 +

67 In digital computers, floating-point numbers consist of three parts:
Floating-point representation A simple model (1/8) In digital computers, floating-point numbers consist of three parts: A sign bit, an exponent part: representing the exponent on a power of 2, a fractional part called a significand: which is a fancy word for a mantissa.

68 More bits used for the exponent increases the range of numbers
Floating-point representation A simple model (2/8) More bits used for the exponent increases the range of numbers More bits used for the significant increases the precision For simplicity, in all this course, we will use a simplified 14 bits model Sign bit: 1 bit Exponent: 5 bits Significand: 8 bits

69 Floating-point representation
A simple model (3/8) Exercise 1: Represent the number 17 in a 14 bits floating point representation 17 = 17.0 x 100 = 1.7 x 101 = 0.17 x 102 Analogically in binary: 1710 = x 20 = x 21= x 22 = x23 = x 24 = x 25 = x 26 = x 27 = ... As a convention, we stop when the MSB of the significant is “1”: x 25 The exponent is 510 = The significant is: → So: 1

70 To solve such problems we use an excess-16 bias
Floating-point representation A simple model (4/8) The last floating point representation is not suitable for negative exponents Example: the number 0.25 = = 0.12 x 2-1 How to represent the negative exponent -1?! To solve such problems we use an excess-16 bias All negative and positive exponents are added by 16 We say that the real exponent is replaced by a biased exponent All exponents are converted to positive biased exponents

71 Floating-point representation
A simple model (5/8) With an excess-16 bias Exponent values less than 16 will indicate negative exponent values Exponent values more than 16 will indicate positive exponent values exponents of all zeros or all ones are typically reserved for special numbers (such as zero or infinity).

72 Floating-point representation
A simple model (6/8) Example 1: Represent the number 17 in a 14 bits floating point form with excess-16 bias The number is positive: sign bit is “0” 1710 = x 25 The exponent is 510 → (5+16)10 = 2110 = The significant is: → So 17 in floating point form with excess-16 bias is: 1

73 Floating-point representation
A simple model (7/8) Example 2: Represent the number in a 14 bits floating point form with excess-16 bias. The number is positive: sign bit is “0” 0.25 = x 20 = 0.12 x 2-1 The exponent is -110 → (-1+16)10 = 1510 = The significant is 1 → So 0.25 in floating point form with excess-16 bias is: 1

74 Floating-point representation
A simple model (8/8) Example 3: Express in normalized floating-point form with excess-16 bias. The number is negative: sign bit is “1” = = x20 = x2-1 = … = 0.1x2-4 The exponent is -410 → (-4+16)10 = 1210 = The significant is 1 → So in floating point form with excess-16 bias is: 1

75 Lecture Overview Introduction Positional Numbering System
Decimal to binary conversion Signed integer representation Floating-point representation A simple model Floating-point arithmetic Floating point errors

76 Floating point arithmetic (1/2)
Floating-point representation Floating point arithmetic (1/2) To add/subtract two numbers in floating point form Both numbers should have the same exponent If exponents are different we change one of the numbers so that both of them are expressed in the same power of the base We add the binary numbers We represent the result in a normalized floating point form

77 Floating point arithmetic (2/2)
Floating-point representation Floating point arithmetic (2/2) Example: Add the following binary numbers as represented in a normalized 14-bit format with an excess-16 bias. The second number is x20 The first number is x22 = x20 Now : The result is x 20 = x 22 In floating point form with excess-16 1810 → 210 1 + 1610 → 010 1

78 Lecture Overview Introduction Positional Numbering System
Decimal to binary conversion Signed integer representation Floating-point representation A simple model Floating-point arithmetic Floating point errors

79 Floating Point Errors (1/2)
Floating-point representation Floating Point Errors (1/2) Computers are finite systems When dealing with floating-point form, we are modeling the infinite system of real numbers in a finite system of integers What we have, in truth, is an approximation of the real number system The more bits we use, the better the approximation However, there is always some element of error Such errors can propagate through a lengthy calculation, causing substantial loss of precision

80 Floating Point Errors (2/2)
Floating-point representation Floating Point Errors (2/2) Example: In our previous simple model we are limited between x215 through x215. we cannot store 2x-19 or 2128; they simply don’t fit. Also, cannot be accurately stored even if it is well within our range = = x28 The significant is expressed with more than 8 bits! In practice we store only the first 8 bits: We actually store 128 and not with an absolute error of 0.5 The relative error is : = = 0.39%. 128.5

81 End of lecture 2 Try to solve all exercises related to lecture 2


Download ppt "Lecture Overview Introduction Positional Numbering System"

Similar presentations


Ads by Google