Sie sind auf Seite 1von 24

Computer Architecture and Organization

Lecture 6: Integers

Majid Khabbazian mkhabbazian@ualberta.ca


Electrical and Computer Engineering University of Alberta

April 9, 2013

Unsigned Integers

Observations

Signed Integers
Many applications require negative values Tows complement form: most common representation of signed numbers Slightly different weights are used
w 2 B 2Tw ( x ) xw1 2 w1 xi 2i i 0

T U

-2w-1 2w-1 Xw-1

2w-2 2w-2 Xw-2

2w-3 2w-3 Xw-3

8 8 x3

4 4 x2

2 2 x1

1 1 x0
4

Observations

Integral Data Types

Examples
x Hex A 0 3 B C F Binary B2U4 B2T4

Other Representations
Ones complement: This is the same as twos complement, except that the most significant bit has the weight -(2w-1-1) rather than -2w-1 than
w 2 B 2Ow ( x ) xw1 (2 w1 1) xi 2i i 0

Sign-magnitude: the most significant w 2 bit is a sign bit

B 2Tw ( x ) (1) xw1 . xi 2i


i 0

Twos-Complement Addition
In binary, add bit patterns as if they were unsigned
Again, discard high order w+1 bit Interpret result as signed (remember bits + context) Most computers use the same machine instruction to perform either unsigned or signed addition This is great!
It is not a coincidence!

Examples
Suppose w=4
What is the range of signed numbers?
[-8,+7]

Examples:
a)3+4=? b)4+6=? c) 6+7 =?

10

Signed Multiplication
Multiply them as if they are unsigned
Truncate the result if needed Interpret the result as twos-complement We can use a single type of multiply instruction
Just like addition Great!

Twos-Complement Negation
-x = ~x+1
x + (~x) + 1 = 2w

Example (w=8) :
a)- (10010111) = b)- (00110110) = c) - (11111111) =

Subtraction
Subtraction is performed using addition
a b = a + (-b)

Example: (w=8)
a) (00100111) (00011011) = b) (00011101) (01001011) =

Conversion
What happens when you assign a signed integer to an unsigned one, or vice versa ?
Bit pattern remains the same, but the bits may be interpreted differently Recall: Information is bits + context

Twos Compliment 2 Unsigned


Let x be a signed number of length w bits How to cast between signed and unsigned?
Use the fact that they have identical bit representations

We can define T2Uw(x)


T2Uw(x) = B2Uw(T2Bw(x))

U2Tw(x) & T2Uw(x)


Rule:
Bit representation is not changed Context (the way you interpret bits) is changed

T U

-2w-1 2w-1 Xw-1

2w-2 2w-2 Xw-2

2w-3 2w-3 Xw-3

8 8 x3

4 4 x2

2 2 x1

1 1 x0

16

Twos Compliment 2 Unsigned


Example: Let x=-127 be a signed number of length 8 bits T2Uw(x) = ?

Unsigned 2 Twos Compliment


Let x be a signed number of length w bits Again we use the fact that
Use the fact that they have identical bit representations

We can define U2Tw(x)


U2Tw(x) = B2Tw(U2Bw(x))

Unsigned 2 Twos Compliment


Example: Let x=228 be a unsigned number of length 8 bits U2Tw(x) = ?

Rational Numbers
How to encode them?
Can we encode all the rational numbers in the interval [0,1]?
What should we do?

20

Fractional Decimal Numbers

Fractional Binary Numbers

Observations
In decimal the period is called a decimal point
In binary the period is now a binary point Digits left of the point are weight by non-negative powers, and digits right of the point are weighted by negative powers Shifting point right multiplies by base Shifting point left divides by base

Another Observation
Numbers of the form 0.b-1b-2b-3... represent values just less than one.
e.g. 0.111112 = 1/2 + 1/4 + 1/8 + 1/16 + 1/32
= 16/32 + 8/32 + 4/32 + 2/32 + 1/32 = 31/32

Das könnte Ihnen auch gefallen