Lecture 2



Lecture 19 Integers, Divisions and Algorithms (Cont.)

Goals and Objectives:

• Understand how signed integers are stored in computer

• Understand 1’s and 2’s complements

• Understand the range of integer

Read 2.4, 2.5, p. 153-179

2. Integer Representations: (32 bits) (Cont.)

2. Signed Integer: use the left most bit as the sign bit

if sign bit=0, then positive #

if sign bit=1, then negative #

A. Positive

(1) Largest

231 - 1

Binary representation: 01111111 11111111 11111111 11111111

(2) Smallest : 1

B. Negative

Decimal change to Binary Algorithm:

(1) Find Binary representation of the positive (or unsigned) value

(2) use the corresponding (1's or 2's complement) technique

(i) 1's complement (change 0 to 1 and 1 to 0)

(ii) 2's complement (the 1's complement representation + 1)

Ex. Using 6 bits to represent integer -12 inside computer

-1210 = ?2

Solution:

(1) Find Binary representation of the positive (or unsigned) value

12 = 0011002

(2)(i) 1's complement

by 1's complement,

Answer: -12 = 110011 in computer using 6 bits

(ii) 2's complement

by 2's complement, -12 = 110011

+ 1

________

110100

Answer: -1210 = 110100 in computer using 6 bits

Example. An 8 bits computer uses 1's complement to represent negative

integers. What are the binary representations of +23 and -23?

What about 2's complement?

Algorithm:

(1) Find the positive signed (or unsigned) representation (Binary)

(Note: the leftmost bit =0 represents + sign)

i.e. +23= 0001 01112

For -23:

(i) 1's complement

apply it to the positive signed integer above using 1's complement

(by interchange 0 with 1 and vice versa)

i.e. -23 = 1110 10002

Note: Computer recognizes 1110 1000 by doing the following:

(a) check left most bit

since the bit is 1, it puts the result sign as -

(b) do 1's complement

the result is 0001 0111, which is 23 in decimal system

Then the answer is -23

(ii) 2's complement

apply it to the positive signed integer above using 2's complement

(by interchange 0 with 1 and vice versa then add 1 to it)

i.e. -23 = 1110 1001

Note: Computer recognizes 1110 1001 by doing the following:

(a) check left most bit

since the bit is 1, it puts the result sign as -

(2) do 2's complement

the result is 0001 0111, which is 23 in decimal system

Then the answer is -23

Disadvantages of using 1's complement for negative integers:

(1) 2 representations for value 0:

Ex. For an 8-bit machine:

+010 = 000000002

-010 = 111111112

However, use 2's complement, 0 has only one representation:

-010 = 000000002 = +010

(2) Do not follow usually arithmetic rules:

Ex. For a 6-bit machine:

-610 = 1110012

-1710 = 1011102

Then

-610 + (-17) 10 = -2310 = 1010002

However,

1110012 + 1011102 [pic] 1010002

On the other hand, use 2's complement, the usual arithmetic

operation such as + follows the usual rule:

-610 = 1110102

-1710 = 1011112

Then

-610 + (-17)10 = -2310 = 1010012

1110102 + 1011112 = 1010012

Summary:

A 8 bits computer have integer range

(1) Unsigned integer

8

0 - 255(= 2 - 1), total 256 representations

The largest integer (255) representation 1111 1111

(2) Signed integer(divide 256 representations into 2 equal parts, one

for + and the other for -)

(i) 1's complement(total 255 representations, missing one representation)

-127 - +127

0111 1111 is the representation of +127

1000 0000 is the representation of -127

(ii) 2's complement(total 256 representations)

-128 - +127

0111 1111 is the representation of +127

1000 0000 is the representation of -128

A 3 bits computer have integer range

(1) Unsigned integer

0 - 7

(2) Signed integer

(i) 1's complement

-3 - +3

(ii) 2's complement

-4 - +3

Ex. Given a 6 bit machine,

(1) What is the largest unsigned integer?

Ans: 26-1 = 63

Ans: 0

(2) What is the smallest unsigned integer?

Ans: 0

(3) What is the largest signed integer using 1's complement?

divide the unsigned integer range (0 - 63) into half of positive and half of negative integers:

we have -31 to -0 and +0 to +31. Note: -0 representation is usually not used.

Ans: 31

(4) What is the smallest signed integer using 1's complement?

Ans: -31

(5) What is the largest signed integer using 2's complement?

divide the unsigned integer range (0 - 63) into half of positive and half of negative integers:

we have -32 to -1 and 0 to +31

Ans: 31

(4) What is the smallest signed integer using 2's complement?

Ans: -32

Assignment #15

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download