50
Representing Integer Data Book : Chapter 4 1 3419 ( Subject has no point !! ) 7 6 5 2 9 9 3 0 0 2 477 666 11011011011011 A99ACF

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

Embed Size (px)

Citation preview

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

Representing Integer Data

Book : Chapter 4

1

3419

( Subject has no point !! )

7652

9930

02

477 666

11011011011011

A99ACF

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

Basic Definition

• An integer is a number which has no fractional part.

Examples: -2022-213

01

66654323434565434

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

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

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

In General (binary)

No. of bits

Binary

Min Max

n 0 2n - 1

Remember !!

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

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., -510 = ?2

• Two common schemes: 1) sign-magnitude 2) two’s complement

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

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

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

Ranges (revisited)

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 8: Representing Integer Data Book : Chapter 4 1 3419 ( Subject has no point !! ) 7652993002 477 666 11011011011011 A99ACF

In General (revisited)

No. of bits

Binary

Unsigned Sign-magnitude

Min Max Min Max

n 0 2n - 1 -(2

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

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

Difficulties with Sign-Magnitude

• Two representations of zero• Using 6-bit sign-magnitude…

•0: 000000•0: 100000

• Arithmetic is awkward!

pp. 95-96

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

Complementary Representations

• 9’s complement

• 10’s complement

• 1’s complement

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

Figure 4.5 Range shifting decimal integers

Range shifting decimal integers – 9’s complement

501

-498

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

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

Numbers Negative Positive

Representation method Complement Number itself

Range of decimal numbers -499 -000 +0 499

Calculation 999 minus number none

Representation example 500 999 0 499

– Increasing value +999 – 499

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

• Find the 9’s Complement representation for the 3 digit number -467

999

-467

---------

532

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

• Find the 9’s Complement representation for the 4 digit number -467

9999

- 467

---------

9 532

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

• Find the 9’s Complement representation for the 4 digit number -3120

9999

-3120

---------

6879

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

Figure 4.6 Addition as a counting process

Addition as a counting process

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

Using 9’s Complement. If we are using 4 bits, the number 5450 represents ?

9999 –

5450

---------

-4549

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

Figure 4.7 Addition with wraparound

Addition with wraparound

999 -300------ 699

200 + (-300) = -100

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

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

Representation 500 799 999 0 99 499

Number

represented

-499 -200 -000 0 100 499

+300

(1099)799

300

1099

1

100

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

Figure 4.10 One’s complement representation

One’s complement representation+-

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

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

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

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 10101100 0101

0010 1101 = 45

1100 0101 = –58

1111 0010 = –13

0010 1101 = 45

0011 1010 = 58

0110 0111 = 103

0000 1101

8 + 4 + 1 = 13Invert to get magnitude

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

Addition with Carry

• 8-bit number

0000 0010 (210) 1111 1101

• Add

• 9 bits

End-around carry

0110 1010 = 106

1111 1101 = –2

10110 0111

+1

0110 1000 = 104

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

Subtraction

• 8-bit number • Invert

0101 1010 (9010) 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

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

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

12610Invert to get magnitude

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

Figure 4.11 Ten’s complement scale

Ten’s complement scale

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

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

- 247

-------

753

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

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

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

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

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

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

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

Figure 4.12 Two’s complement representation

Two’s complement representation

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

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

111010+ 1 111011

-5

000101+5

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

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)

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

“Complementary” Notation

• Conversions between positive and negative numbers are easy

• For binary (base 2)…

+ve -ve

2’s C

2’s C

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

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

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

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:

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

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

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

Detail for -20 -> 11101100

-2010: Positive Value = 00010100

“Flip”: 11101011

Add 1: + 1

11101100

kc

(One’s complement)

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

Detail for 1100011 -> - 29

2’s Complement: 1100011

“Flip”: 0011100

Add One: + 1

0011101

Converts to: = - 29

kc

(One’s complement)

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

Range for 2’s Complement

• For example, 6-bit 2’s complement notation

-32 -31 ... -1 0 1 ... 31

000000111111 000001 011111100000 100001

Negative, sign bit = 1 Zero or positive, sign bit = 0

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

Ranges (revisited)

No. of bits

Binary

Unsigned Sign-magnitude 2’s complement

Min Max Min Max Min Max

1 0 1

2 0 3 -1 1 -2 1

3 0 7 -3 3 -4 3

4 0 15 -7 7 -8 7

5 0 31 -15 15 -16 15

6 0 63 -31 31 -32 31

Etc.

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

In General (revisited)

No. of bits

Binary

Unsigned Sign-magnitude 2’s complement

Min Max Min Max Min Max

n 0 2n - 1 -(2

n-1 - 1) 2n-1

-1 -2n-1

2n-1

- 1

Hint! Learn this table!

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

2’s Complement Addition

• Easy

• No special rules

• Just add

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

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

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

2’s Complement Subtraction

• Easy

• No special rules

• Just subtract, well … actually … just add!

A – B = A + (-B)

add 2’s complement of B

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

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

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

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

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

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

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

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

12610Invert to get magnitude

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

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