Presentation is loading. Please wait.

Presentation is loading. Please wait.

Representing Integer Data Book : Chapter 4 1 3419 ( Subject has no point !! ) 7652993002 477 666 11011011011011 A99ACF.

Similar presentations


Presentation on theme: "Representing Integer Data Book : Chapter 4 1 3419 ( Subject has no point !! ) 7652993002 477 666 11011011011011 A99ACF."— Presentation transcript:

1 Representing Integer Data Book : Chapter 4 1 3419 ( Subject has no point !! ) 7652993002 477 666 11011011011011 A99ACF

2 Basic Definition An integer is a number which has no fractional part. Examples: -2022 -213 0 1 666 54323434565434

3 Ranges for Data Formats Etc. 0 – 9990 – 9999990 – 16,777,21524 0 – 990 – 99990 - 65,53516 0 – 5119 0 – 90 – 990 – 2558 0 – 1277 0 – 636 0 – 315 0 – 90 – 154 0 – 73 0 – 32 0 – 11 ASCIIBCDBinaryNo. of bits

4 In General (binary) No. of bits Binary MinMax n 02 n - 1 Remember !!

5 Signed Integers Previous examples were for “unsigned integers” (positive values only!) Must also have a mechanism to represent “signed integers” (positive and negative values!) E.g., -5 10 = ? 2 Two common schemes: 1) sign-magnitude 2) two’s complement

6 Sign-Magnitude Extra bit on left to represent sign 0 = positive value 1 = negative value E.g., 6-bit sign-magnitude representation of +5 and –5: +5:0 0 0 1 0 1 +ve 5 -5:1 0 0 1 0 1 -ve 5

7 Ranges (revisited) No. of bits Binary UnsignedSign-magnitude MinMaxMinMax 101 2031 307-33 4015-77 5031-1515 6063-3131 Etc.

8 In General (revisited) No. of bits Binary UnsignedSign-magnitude MinMaxMinMax n02 n - 1-(2 n-1 - 1)2 n-1 - 1

9 Difficulties with Sign-Magnitude Two representations of zero Using 6-bit sign-magnitude… 0: 000000 0: 100000 Arithmetic is awkward! pp. 95-96

10 Complementary Representations 9’s complement 10’s complement 1’s complement

11 Figure 4.5 Range shifting decimal integers Range shifting decimal integers – 9’s complement 501 -498

12 9’s Decimal Complement Taking the complement: subtracting a value from a standard basis value Decimal (base 10) system diminished radix complement Radix minus 1 = 10 – 1 9 as the basis 3-digit example: base value = 999 Range of possible values 0 to 999 arbitrarily split at 500 NumbersNegativePositive Representation methodComplementNumber itself Range of decimal numbers-499-000+0499 Calculation999 minus numbernone Representation example500999 0499 – Increasing value+ 999 – 499

13 Find the 9’s Complement representation for the 3 digit number -467 999 -467 --------- 532

14 Find the 9’s Complement representation for the 4 digit number -467 9999 - 467 --------- 9 532

15 Find the 9’s Complement representation for the 4 digit number -3120 9999 -3120 --------- 6879

16 Figure 4.6 Addition as a counting process Addition as a counting process

17 Using 9’s Complement. If we are using 4 bits, the number 5450 represents ? 9999 – 5450 --------- -4549

18 Figure 4.7 Addition with wraparound Addition with wraparound 999 -300 ------ 699 200 + (-300) = -100

19 Addition with End-around Carry Count to the right crosses the modulus End-around carry Add 2 numbers in 9’s complementary arithmetic If the result has more digits than specified, add carry to the result +300 Representation500799999099499 Number represented -499-200-0000100499 +300 (1099) 799 300 1099 1 100

20 Figure 4.10 One’s complement representation One’s complement representation + -

21 Finding one’s complement for a negative Number Invert the bits ! For example one’s complement of -58 using 8 bits 0011 1010 = 58 1100 0101 = -58

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

23 Addition with Carry 8-bit number 0000 0010 (2 10 ) 1111 1101 Add 9 bits End-around carry 0110 1010=106 1111 1101=–2–2 10110 0111 +1 0110 1000=104

24 Subtraction 8-bit number Invert 0101 1010 (90 10 ) 1010 0101 Add 9 bits End-around carry 0110 1010=106 -0101 1010=90 0110 1010=106 –1010 0101=90 10000 1111 +1 0001 0000=16

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

26 Figure 4.11 Ten’s complement scale Ten’s complement scale

27 What is the 3-digit 10’s complement of 247? 1000 - 247 ------- 753

28 Exercises – Complementary Notations What is the 3-digit 10’s complement of 17? Answer: 777 is a 10’s complement representation of what decimal value? Answer: Skip answer Answer

29 What is the 3-digit 10’s complement of 17? Answer: 983 777 is a 10’s complement representation of what decimal value? Answer: 223 1000 - 777 223 Exercises – Complementary Notations Answer

30 Two’s Complement Most common scheme of representing negative numbers in computers Affords natural arithmetic (no special rules!) To represent a negative number in 2’s complement notation… 1.Decide upon the number of bits (n) 2.Find the binary representation of the positive value in n-bits 3.Flip all the bits (change 1’s to 0’s and vice versa) 4.Add 1 Learn! kc

31 Figure 4.12 Two’s complement representation Two’s complement representation

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

33 Sign Bit In 2’s complement notation, the MSB is the sign bit (as with sign-magnitude notation) 0 = positive value 1 = negative value -5: 1 1 1 0 1 1 -ve +5: 0 0 0 1 0 1 +ve 5 ? (previous slide)

34 “Complementary” Notation Conversions between positive and negative numbers are easy For binary (base 2)… +ve -ve 2’s C

35 Example +5 2’s C -5 2’s C +5 0 0 0 1 0 1 1 1 1 0 1 0 + 1 1 1 1 0 1 1 0 0 0 1 0 0 + 1 0 0 0 1 0 1

36 Exercise – 2’s C conversions What is -20 expressed as an 8-bit binary number in 2’s complement notation? Answer: 1100011 is a 7-bit binary number in 2’s complement notation. What is the decimal value? Answer:

37 What is -20 expressed as an 8-bit binary number in 2’s complement notation? Answer: 11101100 1100011 is a 7-bit binary number in 2’s complement notation. What is the decimal value? Answer: -29 Exercise – 2’s C conversions Answer

38 Detail for -20 -> 11101100 -20 10 : PositiveValue = 00010100 “Flip”: 11101011 Add 1: + 1 11101100 kc (One’s complement)

39 Detail for 1100011 -> - 29 2’s Complement:1100011 “Flip”: 0011100 Add One: + 1 0011101 Converts to: = - 29 kc (One’s complement)

40 Range for 2’s Complement For example, 6-bit 2’s complement notation -32 -31... -1 0 1... 31 000000111111000001011111100000100001 Negative, sign bit = 1Zero or positive, sign bit = 0

41 Ranges (revisited) No. of bits Binary UnsignedSign-magnitude2’s complement MinMaxMinMaxMinMax 101 2031-21 307-33-43 4015-77-87 5031-1515-1615 6063-3131-3231 Etc.

42 In General (revisited) No. of bits Binary UnsignedSign-magnitude2’s complement MinMaxMinMaxMinMax n02 n - 1-(2 n-1 - 1)2 n-1 -1-2 n-1 2 n-1 - 1 Hint! Learn this table!

43 2’s Complement Addition Easy No special rules Just add

44 What is -5 plus +5? Zero, of course, but let’s see -5: 10000101 +5: +00000101 10001010 Sign-magnitude -5: 11111011 +5: +00000101 00000000 Twos-complement 11111111

45 2’s Complement Subtraction Easy No special rules Just subtract, well … actually … just add! A – B = A + (-B) add 2’s complement of B

46 What is 10 subtract 3? 7, of course, but… Let’s do it (we’ll use 6-bit values) 10 – 3 = 10 + (-3) = 7 001010 +111101 000111 +3: 000011 1s C: 111100 +1: 1 -3: 111101

47 What is 10 subtract -3? 13, of course, but… Let’s do it (we’ll use 6-bit values) 10 – (-3) = 10 + (-(-3)) = 13 001010 +000011 001101 -3: 111101 1s C: 000010 +1: 1 +3: 000011 (-(-3)) = 3

48 Overflow the result of the calculation does not fit the available number of bits for the result Ex: 1 sign bit, 2 bits for the number 010 + 010 ---------------- 100 Detection: sign of the result is different then the signs of the operands

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

50 Carry the result of an addition (or subtraction) exceeds the allocated bits, independently of the sign Ex: 1 sign bit, 2 bits for the number (carry, not overflow) 010 + 110 ----------- 1000


Download ppt "Representing Integer Data Book : Chapter 4 1 3419 ( Subject has no point !! ) 7652993002 477 666 11011011011011 A99ACF."

Similar presentations


Ads by Google