1 lecture 3 representing integer data itec 1000 “introduction to information technology”

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

Upload: andrew-burke

Post on 19-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

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

1

Lecture 3Lecture 3

Representing Integer DataRepresenting Integer Data

ITEC 1000 “Introduction to Information Technology”

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

2

Lecture Template:Lecture Template:

Number RepresentationNumber Representation Unsigned IntegerUnsigned Integer Signed IntegerSigned Integer Complementary RepresentationComplementary Representation 9’s Decimal Representation (skip)9’s Decimal Representation (skip) 1’s Binary Representation (skip)1’s Binary Representation (skip) 10’s Decimal Representation10’s Decimal Representation 2’s Binary Representation2’s Binary Representation

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

3

Number RepresentationNumber Representation

An integer is a number which has no fractional part.

Numbers can be represented as a combination of Sign (plus or minus) Value or magnitude

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

4

Unsigned IntegerUnsigned Integer(Natural Number)(Natural Number)

8-bit storage location28 different values between 0 and 255

16-bit storage location216 different values between 0 and 65535

multiple storage locations4 consecutive 1-byte storage locationsprovide 32 bits of range 232, or 4,294,967,296 different valuesdifficult to calculate and manipulate

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

5

32-bit multiple storage location32-bit multiple storage location

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

6

Signed-Integer RepresentationSigned-Integer Representation

No obvious direct way to represent the sign in binary notation

Options:Sign-and-magnitude representation1’s complement (skip – confusing)2’s complement (most common)

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

7

Sign-and-MagnitudeSign-and-Magnitude

Use left-most bit for sign0 = plus; 1 = minus

Total range of integers the sameHalf of integers positive; half negativeMagnitude of largest integer half as large

Example using 8 bits:Unsigned: 1111 1111 = +255Signed: 0111 1111 = +127

1111 1111 = -127Note: 2 values for 0:

+0 (0000 0000) and -0 (1000 0000)

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

8

Calculation AlgorithmsCalculation Algorithms

Sign-and-magnitude algorithms complex and difficult to implement in hardware

Must test for 2 values of 0Useful with BCD (see slides at end)Order of signed number and carry/borrow makes a difference

Example: Decimal addition algorithm

Addition: 2 Positive Numbers

Addition: 1 Signed Number

4+2

6

4- 2

2

2- 4-2

12- 4

8

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

9

RangesRanges

No. of bits

Binary

Unsigned Sign-magnitude

Min Max Min Max

1 0 1

2 0 3 -1 1

3 0 7 -3 3

4 0 15 -7 7

5 0 31 -15 15

6 0 63 -31 31

Etc.

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

10

Ranges: General RuleRanges: General Rule

No. of bits

Binary

Unsigned Sign-magnitude

Min Max Min Max

n 0 2n - 1 -(2

n-1 - 1) 2n-1 - 1

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

11

Complementary RepresentationComplementary Representation

Sign of the number does not have to be handled separately

Consistent for all different signed combinations of input numbers

Two methodsRadix: value used is the base numberDiminished 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

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

12

10’s Decimal Complement10’s Decimal Complement

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 = 1000Range of possible values 0 to 999 arbitrarily split at 500

Numbers Negative Positive

Representation method Complement Number itself

Range of decimal numbers -500 -1 +0 499

Calculation 1000 minus abs(number) none

Representation example 500 999 0 499

– Increasing value +1000 – 500

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

13

10’s Decimal Complement10’s Decimal Complement

Necessary to specify number of digits or word size

10’s complement representation in “1000” base

Example: representation of 3-digit number

First digit = 0 through 4 positive numberFirst digit = 5 through 9 negative number

1- 500- if ,)( -

499 0 if ,

valuevalueabsbase

valuevaluecomprep

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

14

Examples with 3-Digit NumbersExamples with 3-Digit Numbers

Example 1:10’s complement representation of 247

247 (positive number)

Example 2:10’s complement representation of -247

1000 – 247 = 753 (negative number)

Example 3:Find a value with 10’s complement representation 777

Negative number because first digit is 71000 – 777 = 223Signed value = -223

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

15

10’s Decimal Complement10’s Decimal Complement

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

321 remains +321521: take the difference: base minus complement (999 – 521) with negative sign: – 479

999 500 if ), (

499 0 if ,

comprepcomprepbase

comprepcomprepvalue

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

16

Addition: Counting Upwards Addition: Counting Upwards

Counting upward on scale corresponds to addition Example in 10’s complement: does not cross the

modulus

+250 +250

Representation

500 650 900 999 0 170 420 499

Numberrepresented

-500 -350 -100 -1 +0 170 420 499

+250 +250

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

17

Addition with WraparoundAddition with Wraparound

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 500 999 0 200 499 500 900 999

Numberrepresented

-500 -1 0 200 499 -500 -100 -1

-300

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

18

Addition with End-around CarryAddition with End-around Carry

Count to the right crosses the modulus

+300

Representation 500 800 999 0 100 499

Numberrepresented

-500 -200 -1 0 100 499

+300

800300

1100

100

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

19

OverflowOverflow

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 Test: If both inputs to an addition have the

same sign and the output sign is different, an overflow occurred

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

20

2’s Binary Complement2’s Binary Complement

Binary system complementBinary (base 2) system radix complement Radix = 2 2 as the basis

Inversion: change 1’s to 0’s and 0’s to 1s (add 1)Numbers beginning with 0 are positiveNumbers beginning with 1 are negative

Example with 8-bit binary numbers

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

21

2’s Complement2’s Complement

2’s complement representation:Positive value represents itselfNegative value: invert and add “1”

Numbers Negative Positive

Representation method Complement Number itself

Range of decimal numbers -12810 -110 +010 12710

Calculation Inversion None

Representation example 10000000 11111111 00000000 01111111

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

22

Example: 2’s ComplementExample: 2’s Complement

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. Invert all the bits:

4. Add 1 111010+ 1 111011

-5

000101+5

6 (for example)

111010

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

23

Sign Bit in 2’s ComplementSign Bit in 2’s Complement

In 2’s complement representation, the MSB is the sign bit (as with sign-magnitude notation)

0 = positive value1 = negative value

-5: 1 1 1 0 1 1

-ve

+5: 0 0 0 1 0 1

+ve 5

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

24

Estimating Integer Value of 2’s Estimating Integer Value of 2’s Complement RepresentationComplement Representation

Positive numbers begin with 0 Small negative numbers (close to 0)

begin with multiple 1’s1111 1110 = -2 in 8-bit 2’s complements1000 0000 = -128, largest negative 2’s complementsInvert all 1’s and 0’s, add “1” and approximate the value

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

25

Exercise: 2’s complement Exercise: 2’s complement conversionsconversions

What is -20 expressed as an 8-bit binary number in 2’s complement representation?

Answer:

1100011 is a 7-bit binary number in 2’s complement representation. What is the decimal sign-and-magnitude value?

Answer:

Skip answer Answer

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

26

Exercise: 2’s complement Exercise: 2’s complement conversionsconversions

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

Answer

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

27

Detail for -20 -> 11101100Detail for -20 -> 11101100

-2010: Positive Value = 00010100

Invert: 11101011

Add 1: + 1

11101100

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

28

Detail for 1100011 -> - 29Detail for 1100011 -> - 29

2’s Complement Rep: 1100011

Invert: 0011100

Add One: + 1

0011101

Converts to: = - 29

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

29

Arithmetic in 2’s ComplementArithmetic in 2’s Complement

Add 2 positive 8-bit numbers

Add 2 8-bit numbers with different signs Take the 1’s

complement of 58 (i.e. invert, add 1)0011 10101100 0110

0010 1101 = 45

1100 0110 = –58

1111 0011 = –13

0010 1101 = 45

0011 1010 = 58

0110 0111 = 103

0000 1101

8 + 4 + 1 = 13Invert to get magnitude

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

30

Addition with CarryAddition with Carry in 2’s in 2’s ComplementComplement

8-bit number Invert (add 1)0000 0010 (210) 1111 1110Adddrop final carry out

0110 1010 = 106

1111 1110 = –2

10110 1000 = 104

(drop 1)

0110 1000

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

31

SubtractionSubtraction

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

Adddrop final carry out

0110 1010 = 106

-0101 1010 = 90

0110 1010 = 106

–1010 0110 = 90

10001 0000

(drop 1)

0001 0000 = 16

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

32

OverflowOverflow

8-bit number 256 different numbersPositive numbers: 0 to 127

AddTest 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 -127

0111 1110

12710Invert to get magnitude

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

33

Overflow and Carry ConditionsOverflow and Carry Conditions

Carry flag: set when the result of an addition or subtraction exceeds fixed number of bits allocated

Overflow: result of addition or subtraction overflows into the sign bit

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

34

Overflow/Carry ExamplesOverflow/Carry Examples

Example 1: Correct resultNo overflow, no carry

Example 2:Incorrect resultOverflow, no carry

0100 = (+ 4)

0010 = + (+ 2)

0110 = (+ 6)

0100 = (+ 4)

0110 = + (+ 6)

1010 = (– 6)

0101+ 10110

Invert, then add 1 to get magnitude

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

35

Overflow/Carry ExamplesOverflow/Carry Examples

Example 3: Result correct ignoring the carryCarry but no overflow

Example 4:Incorrect resultOverflow, carry ignored

1100 = (– 4)

1110 = + (– 2)

11010 = (– 6)

1100 = (– 4)

1010 = + (– 6)

10110 = (+ 3)

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

36

2’s Complement Subtraction2’s Complement Subtraction

Just add the opposite value!

A – B = A + (-B)

add 2’s complement rep. of -B

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

37

Not quite finished => BCDNot quite finished => BCD

Reading: Lecture slides and Chapter 5Reading: Lecture slides and Chapter 5

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

38

Unsigned IntegersUnsigned Integers

Binary equivalentconversion as discussed in Lecture 2 4 bits can hold 16 different values 0 – 158 bits can hold 28 different values between 0 and 255

BCD: Binary-Coded Decimaldigit-by-digit individual conversion to binary4 bits per decimal digit, i.e. 10 different values 0 - 98-bit storage location can hold 2 BCD digits, i.e. 100 different values 00 - 99

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

39

Binary vs. BCDBinary vs. BCD

Decimal Binary BCD

68 = 0100 0100 = 0110 1000

= 26 + 22 = 64 + 4 = 68 = 22 + 21 = 6 23 = 8

99(largest 8-bit

BCD)

= 0110 0011 = 1001 1001

= 26 + 25 + 21 + 20 == 64 + 32 + 2 + 1 = 99

= 23 + 20 23 + 20 = 9 9

255 (largest 8-bit

binary)

= 1111 1111 = 0010 0101 0101

= 28 – 1 = 2558 bits

= 21 22 + 20 22 + 20

= 2 5 512 bits

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

40

Value Range: Binary vs. BCDValue 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,999 8 digits 0-4 billion 9+ digits

64 0-(1016-1) 16 digits 0-16 quintillion 19+ digits

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

41

Simple BCD MultiplicationSimple BCD Multiplication

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

42

Conventional Binary vs. BCDConventional Binary vs. BCD

Binary representation generally preferredgreater range of value for given number of bitscalculations easier

BCD is still usedin business applications to maintain decimal rounding and decimal precisionin applications with a lot of input and output, but limited calculations