ITEC 1000 Introduction to Information Technology Lecture 3

  • Slides: 42
Download presentation
ITEC 1000 “Introduction to Information Technology” Lecture 3 Representing Integer Data 1

ITEC 1000 “Introduction to Information Technology” Lecture 3 Representing Integer Data 1

Lecture Template: Number Representation n Unsigned Integer n Signed Integer n Complementary Representation n

Lecture Template: Number Representation n Unsigned Integer n Signed Integer n Complementary Representation n 9’s Decimal Representation (skip) n 1’s Binary Representation (skip) n 10’s Decimal Representation n 2’s Binary Representation n 2

Number Representation An integer is a number which has no fractional part. n Numbers

Number Representation An integer is a number which has no fractional part. n Numbers can be represented as a combination of n Sign (plus or minus) n Value or magnitude n 3

Unsigned Integer (Natural Number) n 8 -bit storage location 28 different values between 0

Unsigned Integer (Natural Number) n 8 -bit storage location 28 different values between 0 and 255 n 16 -bit storage location 216 different values between 0 and 65535 n multiple storage locations 4 consecutive 1 -byte storage locations provide 32 bits of range 232, or 4, 294, 967, 296 different values difficult to calculate and manipulate 4

32 -bit multiple storage location 5

32 -bit multiple storage location 5

Signed-Integer Representation No obvious direct way to represent the sign in binary notation n

Signed-Integer Representation No obvious direct way to represent the sign in binary notation n Options: n Sign-and-magnitude representation n 1’s complement (skip – confusing) n 2’s complement (most common) n 6

Sign-and-Magnitude n Use left-most bit for sign n n Total range of integers the

Sign-and-Magnitude n Use left-most bit for sign n n Total range of integers the same n n n 0 = plus; 1 = minus Half of integers positive; half negative Magnitude of largest integer half as large Example using 8 bits: n n n Unsigned: 1111 = +255 Signed: 0111 1111 = +127 1111 = -127 Note: 2 values for 0: +0 (0000) and -0 (1000 0000) 7

Calculation Algorithms n Sign-and-magnitude algorithms complex and difficult to implement in hardware Must test

Calculation Algorithms n Sign-and-magnitude algorithms complex and difficult to implement in hardware Must test for 2 values of 0 Useful with BCD (see slides at end) Order of signed number and carry/borrow makes a difference n Example: Decimal addition algorithm Addition: 2 Positive Numbers 1 Signed Number 4 +2 6 4 -2 2 2 -4 -2 12 -4 8 8

Ranges No. of bits 1 2 3 4 5 6 Etc. Binary Unsigned Sign-magnitude

Ranges No. of bits 1 2 3 4 5 6 Etc. Binary Unsigned Sign-magnitude Min Max 0 1 0 0 0 3 7 15 31 63 -1 -3 -7 -15 -31 1 3 7 15 31 9

Ranges: General Rule Binary No. of bits n Unsigned Min Max 0 n 2

Ranges: General Rule Binary No. of bits n Unsigned Min Max 0 n 2 1 Signmagnitude Min Max n-1 -(2 - 2 1) 1 10

Complementary Representation Sign of the number does not have to be handled separately n

Complementary Representation Sign of the number does not have to be handled separately n Consistent for all different signed combinations of input numbers n Two methods n Radix: value used is the base number Diminished radix: value used is the base number minus 1 (skip – confusing) 9’s complement: base 10 diminished radix 1’s complement: base 2 diminished radix 11

10’s Decimal Complement n n Complement representation: (1) positive number: remains itself; (2) negative

10’s Decimal Complement n n Complement representation: (1) positive number: remains itself; (2) negative number: subtracting its absolute value from a standard basis value Decimal (base 10) system: radix complement Radix = 10 10 as the basis 3 -digit example: base value = 1000 Range of possible values 0 to 999 arbitrarily split at 500 Numbers Representation method Range of decimal numbers Calculation Negative Positive Complement Number itself -500 -1 +0 none 1000 minus abs(number) Representation example 1000 – 500 – 999 499 0 Increasing value 499 + 12

10’s Decimal Complement n Necessary to specify number of digits or word size n

10’s Decimal Complement n Necessary to specify number of digits or word size n 10’s complement representation in “ 1000” base n Example: representation of 3 -digit number First digit = 0 through 4 First digit = 5 through 9 positive number negative number 13

Examples with 3 -Digit Numbers n Example 1: 10’s complement representation of 247 (positive

Examples with 3 -Digit Numbers n Example 1: 10’s complement representation of 247 (positive number) n Example 2: 10’s complement representation of -247 1000 – 247 = 753 (negative number) n Example 3: Find a value with 10’s complement representation 777 Negative number because first digit is 7 1000 – 777 = 223 Signed value = -223 14

10’s Decimal Complement n Conversion to sign-and-magnitude value for 9’s complement representation(3 digits) n

10’s Decimal Complement n Conversion to sign-and-magnitude value for 9’s complement representation(3 digits) n 321 remains +321 n 521: take the difference: base minus complement (999 – 521) with negative sign: – 479 15

Addition: Counting Upwards n n Counting upward on scale corresponds to addition Example in

Addition: Counting Upwards n n Counting upward on scale corresponds to addition Example in 10’s complement: does not cross the modulus +250 Representation Number represented 500 650 900 999 -500 -350 -100 -1 +250 0 170 420 499 +250 16

Addition with Wraparound n n Count to the right to add a negative number

Addition with Wraparound n n Count to the right to add a negative number to the representation (since the complement 700 represents the value of -300) Wraparound scale used to extend the range for the negative result +700 Representation Number represented 500 999 0 200 499 500 999 -500 -1 0 200 499 -500 -1 -300 17

Addition with End-around Carry n Count to the right crosses the modulus +300 Representation

Addition with End-around Carry n Count to the right crosses the modulus +300 Representation Number represented 500 800 999 0 100 499 800 300 -500 -200 -1 0 100 499 1100 +300 18

Overflow n n Fixed word size has a fixed range size Overflow: combination of

Overflow n n Fixed word size has a fixed range size Overflow: combination of numbers that adds to result outside the range End-around carry in modular arithmetic avoids problem Complementary arithmetic: numbers out of range have the opposite sign n Test: If both inputs to an addition have the same sign and the output sign is different, an overflow occurred 19

2’s Binary Complement n Binary system complement Binary (base 2) system radix complement Radix

2’s Binary Complement n Binary system complement Binary (base 2) system radix complement Radix = 2 2 as the basis n n Inversion: change 1’s to 0’s and 0’s to 1 s (add 1) Numbers beginning with 0 are positive Numbers beginning with 1 are negative Example with 8 -bit binary numbers 20

2’s Complement n 2’s complement representation: Positive value represents itself Negative value: invert and

2’s Complement n 2’s complement representation: Positive value represents itself Negative value: invert and add “ 1” Numbers Representation method Range of decimal numbers Calculation Representation example Negative Positive Complement Number itself -12810 -110 Inversion 10000000 1111 +010 12710 None 0000 01111111 21

Example: 2’s Complement n Represent – 5 in binary using 2’s complement notation 1.

Example: 2’s Complement n Represent – 5 in binary using 2’s complement notation 1. Decide on the number of bits: 6 (for example) 2. Find the binary representation of the +ve value in 6 bits 000101 +5 3. Invert all the bits: 111010 4. Add 1 111010 + 1 111011 -5 22

Sign Bit in 2’s Complement n In 2’s complement representation, the MSB is the

Sign Bit in 2’s Complement n In 2’s complement representation, the MSB is the sign bit (as with sign-magnitude notation) 0 = positive value n 1 = negative value n +5: 0 0 0 1 +ve 5 -5: 1 1 1 0 1 1 -ve 23

Estimating Integer Value of 2’s Complement Representation Positive numbers begin with 0 n Small

Estimating Integer Value of 2’s Complement Representation Positive numbers begin with 0 n Small negative numbers (close to 0) begin with multiple 1’s n 1111 1110 = -2 in 8 -bit 2’s complements 1000 0000 = -128, largest negative 2’s complements Invert all 1’s and 0’s, add “ 1” and approximate the value 24

Exercise: 2’s complement conversions n What is -20 expressed as an 8 -bit binary

Exercise: 2’s complement conversions n What is -20 expressed as an 8 -bit binary number in 2’s complement representation? Answer: n 1100011 is a 7 -bit binary number in 2’s complement representation. What is the decimal sign-and-magnitude value? Answer: Skip answer Answer 25

Exercise: 2’s complement conversions Answer n What is -20 expressed as an 8 -bit

Exercise: 2’s complement conversions Answer n What is -20 expressed as an 8 -bit binary number in 2’s complement notation? n n Answer: 11101100011 is a 7 -bit binary number in 2’s complement notation. What is the decimal value? n Answer: -29 26

Detail for -20 -> 11101100 -2010: Positive Value = 00010100 Invert: 11101011 Add 1:

Detail for -20 -> 11101100 -2010: Positive Value = 00010100 Invert: 11101011 Add 1: + 1 11101100 27

Detail for 1100011 -> - 29 2’s Complement Rep: 1100011 Invert: 0011100 Add One:

Detail for 1100011 -> - 29 2’s Complement Rep: 1100011 Invert: 0011100 Add One: + 1 0011101 Converts to: = - 29 28

Arithmetic in 2’s Complement n n Add 2 positive 8 bit numbers Add 2

Arithmetic in 2’s Complement n n Add 2 positive 8 bit numbers Add 2 8 -bit numbers with different signs n 0010 1101 = 45 0011 1010 = 0110 0111 = 58 103 0010 1101 = 45 Take the 1’s 1100 0110 = – 58 complement of 1111 0011 = – 13 58 (i. e. invert, add 1) 0000 1101 Invert to get 0011 1010 magnitude 8 + 4 + 1 = 13 1100 0110 29

Addition with Carry in 2’s Complement n 8 -bit number n n n Invert

Addition with Carry in 2’s Complement n 8 -bit number n n n Invert (add 1) 0000 0010 (210) 1111 1110 Add drop final carry out 0110 1010 = 106 1111 1110 = 10110 1000 = (drop 1) – 2 104 0110 1000 30

Subtraction n 8 -bit number n n n Invert (add 1) 0101 1010 (9010)

Subtraction n 8 -bit number n n n Invert (add 1) 0101 1010 (9010) 1010 0110 Add drop final carry out 0110 1010 = 106 -0101 1010 = 90 0110 1010 = 106 – 1010 0110 = 90 10001 0000 (drop 1) 0001 0000 = 16 31

Overflow n 8 -bit number 256 different numbers Positive numbers: 0 to 127 0100

Overflow n 8 -bit number 256 different numbers Positive numbers: 0 to 127 0100 0000 = 64 0100 0001 = 65 1000 0001 -127 n Add n Programmers beware: some high-level languages, e. g. , some versions of BASIC, do not check for overflow adequately 0111 1110 Test for overflow Invert to get 2 positive inputs 12710 produced negative magnitude result overflow! Wrong answer! 32

Overflow and Carry Conditions Carry flag: set when the result of an addition or

Overflow and Carry Conditions Carry flag: set when the result of an addition or subtraction exceeds fixed number of bits allocated n Overflow: result of addition or subtraction overflows into the sign bit n 33

Overflow/Carry Examples n Example 1: n n n Correct result No overflow, no carry

Overflow/Carry Examples n Example 1: n n n Correct result No overflow, no carry Example 2: n n Incorrect result Overflow, no carry Invert, then add 1 to get magnitude 0100 = (+ 4) 0010 = + (+ 2) 0110 = (+ 6) 0100 = (+ 4) 0110 = + (+ 6) 1010 = (– 6) 0101 + 1 0110 34

Overflow/Carry Examples n Example 3: n n n Result correct ignoring the carry Carry

Overflow/Carry Examples n Example 3: n n n Result correct ignoring the carry Carry but no overflow Example 4: n n Incorrect result Overflow, carry ignored 1100 = (– 4) 1110 = + (– 2) 11010 = (– 6) 1100 = (– 4) 1010 = + (– 6) 10110 = (+ 3) 35

2’s Complement Subtraction n Just add the opposite value! A – B = A

2’s Complement Subtraction n Just add the opposite value! A – B = A + (-B) add 2’s complement rep. of -B 36

Reading: Lecture slides and Chapter 5 Not quite finished => BCD 37

Reading: Lecture slides and Chapter 5 Not quite finished => BCD 37

Unsigned Integers n Binary equivalent n n conversion as discussed in Lecture 2 4

Unsigned Integers n Binary equivalent n n conversion as discussed in Lecture 2 4 bits can hold 16 different values 0 – 15 8 bits can hold 28 different values between 0 and 255 BCD: Binary-Coded Decimal n n n digit-by-digit individual conversion to binary 4 bits per decimal digit, i. e. 10 different values 0 -9 8 -bit storage location can hold 2 BCD digits, i. e. 100 different values 00 - 99 38

Binary vs. BCD Decimal Binary BCD = 0100 = 0110 = 26 + 22

Binary vs. BCD Decimal Binary BCD = 0100 = 0110 = 26 + 22 = 64 + 4 = 68 = 2 2 + 2 1 = 6 23 = 8 99 (largest 8 -bit BCD) = 0110 0011 = 1001 = 26 + 25 + 2 1 + 2 0 = = 64 + 32 + 1 = 99 = 23 + 2 0 = 9 23 + 2 0 9 255 (largest 8 -bit binary) = 1111 = 0010 0101 = 28 – 1 = 255 8 bits = 2 1 22 + 2 0 = 2 5 5 12 bits 68 1000 39

Value Range: Binary vs. BCD No. of Bits BCD Range Binary Range 4 0

Value Range: Binary vs. BCD No. of Bits BCD Range Binary Range 4 0 -9 1 digit 0 -15 1+ digit 8 0 -99 2 digits 0 -255 2+ digits 12 0 -999 3 digits 0 -4, 095 3+ digits 16 0 -9, 999 4 digits 0 -65, 535 4+ digits 20 0 -99, 999 5 digits 0 -1 million 6 digits 24 0 -999, 999 6 digits 0 -16 million 7+ digits 32 0 -99, 999 8 digits 0 -4 billion 9+ digits 64 0 -(1016 -1) 16 digits 0 -16 quintillion 19+ digits 40

Simple BCD Multiplication 41

Simple BCD Multiplication 41

Conventional Binary vs. BCD n Binary representation generally preferred n n n greater range

Conventional Binary vs. BCD n Binary representation generally preferred n n n greater range of value for given number of bits calculations easier BCD is still used n n in business applications to maintain decimal rounding and decimal precision in applications with a lot of input and output, but limited calculations 42