Positional integer number representation

CMPE12 – More about Numbers
Integers and
Floating Point
(Rest of Textbook Chapter 2)
CMPE12 – Winter 2008 – J. Ferguson
Review: Unsigned Integer
• A string of 0s and 1s that represent a
positive integer.
• String is Xn-1, Xn-2, … X1, X0, where Xk
is either a 0 or a 1 and has a weight
of 2k.
• The represented number is the sum
of all the weights for each 1 in the
string.
CMPE12 – Winter 2008 – J. Ferguson
16 - 2
Hex and Octal can be used to
represent Binary!
We are only interested in Base 16
(Hexadecimal) and Base 8 (Octal) because
they are a shortened way to represent a
string of 0s and 1s.
CMPE12 – Winter 2008 – J. Ferguson
16 - 3
Signed Integers
Allow us to represent positive and negative integers.
4 important types:
Sign and Magnitude -- Leftmost bit is the sign and
the remaining bits are the unsigned magnitude.
1’s complement -- The additive inverse of a
number is the bit-wise complement of the number.
2’s complement -- The additive inverse of a
number is the bit-wise complement plus one to the
number.
Bias or excess notation -- a bias is subtracted
from the “unsigned value” to get the bias value.
CMPE12 – Winter 2008 – J. Ferguson
16 - 4
Sign-and-magnitude
CMPE12 – Winter 2008 – J. Ferguson
16 - 5
One’s complement
CMPE12 – Winter 2008 – J. Ferguson
16 - 6
Two’s complement
CMPE12 – Winter 2008 – J. Ferguson
16 - 7
More Examples
CMPE12 – Winter 2008 – J. Ferguson
16 - 8
Biased notation
• How does it work? The signed
integer is biased so that the bias
value is equal to 0.
• Advantages
– Preserves lexical order
– Single zero
– Most versatile
• Disadvantages:
- Add and sub require one additional operation
to adjust the bias
CMPE12 – Winter 2008 – J. Ferguson
x
y
000
-3
001
-2
010
-1
011
0
100
1
101
2
110
3
111
4
Bias-3
16 - 9
Biased notation -- detail
• Example 4-bit number bias 7. 1011=4 Why?
0000=-7…
• 0000…000 < everything else
• After adding (subtracting)two numbers you
have to subtract (add) the bias to get the
correct value.
CMPE12 – Winter 2008 – J. Ferguson
16 - 10
Why add or subtract the bias?
What you get
unsigned
(x-k)
(x-k)
x
+(y-k)
+(y-k)
+y
(z-k)
(z-2k)
z
What you want
z-2k
- k
z- k
So you must subtract out the Bias (in this
case, k) when you are finished!
CMPE12 – Winter 2008 – J. Ferguson
16 - 11
Addition and subtraction
in biased notation
Add/subtract normally, then subtract the bias
• Ex: A+B, with A = 2, B = 1, on 3 bits
• Ex: A-B, with A = 1, B = 3, on 3 bits
CMPE12 – Winter 2008 – J. Ferguson
16 - 12
Biased notation mapping
Number
represented
Number 000
encoded
001
010
Range on
n bits: 2n-1-1 to 2n-1
CMPE12 – Winter 2008 – J. Ferguson
011
100
101
110
111
If Bias is 011…11!
16 - 13
Examples
CMPE12 – Winter 2008 – J. Ferguson
16 - 14
Overflow for Bias-numbers
•
•
•
•
•
P+P=P
N+N=N
P-P=?
N-N=?
Rules the same, but 0 (1) msb means
positive (negative).
CMPE12 – Winter 2008 – J. Ferguson
16 - 15
Representation is too small for
the register
Unsigned Integers: how do you put 1010 into
a 16 bit register?
2’s Complement: Sign-Extension. “Extend”
the sign bit out to the end of the register
For a bias number? Xn-1 Xn … X1, X0,
0s if positive
1s if negative
CMPE12 – Winter 2008 – J. Ferguson
16 - 16
bias representation to
“number”
1101 1001 0000 1101 (bias 7FFF) = ?
So 1101 1001 0000 1101 is
1101 1001 0000 1101 - 0111 1111 1111 1111
= 0101 1001 0000 1110
For positive numbers -- subtract the bias
Simpler: negate MSB and add 1.
0111 1111 1111 1111 = 010 therefore
1000 0000 0000 0000 = 110
CMPE12 – Winter 2008 – J. Ferguson
16 - 17
bias representation to
“number”
0101 1101 0000 1101 (bias 7FFF) =
?
0111 1111 1111 1111 = 010 so
How far is 5D0D from 0?
7FFF
-5D0D
CMPE12 – Winter 2008 – J. Ferguson
16 - 18
CMPE12 – Winter 2008 – J. Ferguson
16 - 19
Recommended exercises
• Ex 2.39, 2.40, 2.41, 2.56 (Floating Point
Numbers)
• Ex 2.51, 2.52 (Data types as binary
strings)
CMPE12 – Winter 2008 – J. Ferguson
16 - 20