Beruflich Dokumente
Kultur Dokumente
Lecture 6: Integers
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
8 8 x3
4 4 x2
2 2 x1
1 1 x0
4
Observations
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
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
T U
8 8 x3
4 4 x2
2 2 x1
1 1 x0
16
Rational Numbers
How to encode them?
Can we encode all the rational numbers in the interval [0,1]?
What should we do?
20
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