Download Integers and Floating Point

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CMPE12 – More about Numbers
Integers and
Floating Point
(Rest of Textbook Chapter 2)
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
Quick Review of Signed
Integers
Decimal
1’s
complement
2’s
complement
Sign-andMagnitude
22
0010110
0010110
0010110
-22
1101001
1101010
1010110
CMPE12 – Winter 2008 – J. Ferguson
16 - 5
Biased notation
•  How does it work? The signed
integer is biased so that the bias
value is represented by 000…000.
•  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
Rep
Value
000
-3
001
-2
010
-1
011
0
100
1
101
2
110
3
111
4
Bias 3
16 - 6
Conversion: Decimal/BiasX
•  Decimal -> BiasX
–  Add X, then Convert
to Binary
•  BiasX -> Decimal
–  Convert Binary to
Decimal, then
Subtract X
CMPE12 – Winter 2008 – J. Ferguson
Rep
D
Value
000
0
-3
001
1
-2
010
2
-1
011
3
0
100
4
1
101
5
2
110
6
3
111
7
4
16 - 7
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 - 8
Why add or subtract the bias?
What you get
unsigned
(x-k)
(x-k)
x
-(y-k)
-(y-k)
-y
(z-k)
(z-0)
z
What you want
z-0
+ k
z-k
So you must add back the Bias, k, when you
are finished!
CMPE12 – Winter 2008 – J. Ferguson
16 - 9
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…112
16 - 10
32-bit word
•  32 bit word can represent ~ 4.3
billion values
–  Integers: 0 -> ~4.3 billion
–  Signed Integers: ~ -2.15B -> 2.15B
•  Fractional numbers?
•  Very large numbers?
•  Numbers with very small magnitude?
CMPE12 – Winter 2008 – J. Ferguson
16 - 11
Scientific Notation for large
and small numbers
•  Example: 6.023*1023
•  Of form A.xxx *(BASE)exponent
•  In Binary: 1.xxx… * 2exponent
–  Or maybe Y16.xx…16 * 16exponent
•  Standard: IEEE standard for floating
point arithemetic
CMPE12 – Winter 2008 – J. Ferguson
16 - 12
IEEE standard for floating point
1.xxx * 2exponent in a 32-bit word
The 1. and the 2 can be assumed.
xxx…xx and exponent (and sign) is all
that must be specified.
CMPE12 – Winter 2008 – J. Ferguson
16 - 13
Floating Point Numbers
8 bits
23 bits
S Exponent
1 means
negative
Fraction (xx…xx)
(In Bias 127)
How do we convert to Decimal?
If 00000000 < Exponent < 111111111
N = (-1)S * 1.Fraction * 2Exponent-127
CMPE12 – Winter 2008 – J. Ferguson
16 - 14
Floating Point Numbers
1 10000011 100000……………………………..00000
What number is this?
0 01111100 111000……………………………..00000
What number is this?
CMPE12 – Winter 2008 – J. Ferguson
16 - 15
Converting from Decimal to Float
1.  Convert to Binary (eg. -10010.01101)
2.  Normalize (form = 1.xxxxxx *2EXP)
3.  Convert EXP to bias127 (add 127 to it)
4.  MSB [31] gets sign
5.  [23:30] gets EXP (bias127)
6.  [0:22] gets xxxxxxxxxxxxxxxxxxxxxxx
CMPE12 – Winter 2008 – J. Ferguson
16 - 16
Convert to IEEE FP
•  56.5
•  -5.625
•  -.0004 (do to 5 binary places)
CMPE12 – Winter 2008 – J. Ferguson
16 - 17
Exercises due Feb. 25
•  Ex 2.39, 2.40, 2.41, 2.56
CMPE12 – Winter 2008 – J. Ferguson
16 - 18