University of Washington
Sec.on 2: Integer & Floa.ng Point Numbers
¢
Representa.on of integers: unsigned and signed
¢
Unsigned and signed integers in C
¢
Arithme.c and shiBing
¢
Sign extension
¢
Background: frac.onal binary numbers
¢
IEEE floa.ng-‐point standard
¢
Floa.ng-‐point opera.ons and rounding
¢
Floa.ng-‐point in C
Frac.onal Values
University of Washington
Frac.onal Binary Numbers
¢
What is 1011.101
2
?
¢
How do we interpret frac.onal decimal numbers?
§
e.g. 107.95
10
§
Can we interpret frac9onal binary numbers in an analogous way?
Frac.onal Values
University of Washington
• •
•
b
–1
.
Frac.onal Binary Numbers
Frac.onal Values
¢
Representa.on
§
Bits to right of “binary point” represent frac9onal powers of 2
§
Represents ra9onal number:
b
i
b
i–1
b
2
b
1
b
0
b
–2
b
–3
b
–j
• • •
• • •
1
2
4
2
i–1
2
i
• • •
1/2
1/4
1/8
2
–j
b
k
⋅2
k
k =− j
i
∑
University of Washington
Frac.onal Binary Numbers: Examples
¢
Value
Representa.on
§
5 and 3/4
§
2 and 7/8
§
63/64
¢
Observa.ons
§
Divide by 2 by shiMing right
§
Mul9ply by 2 by shiMing leM
§
Numbers of the form 0.111111…
2
are just below 1.0
§
1/2 + 1/4 + 1/8 + … + 1/2
i
+ … → 1.0
§
Shorthand nota9on for all 1 bits to the right of binary point:
1.0 –
ε
Frac.onal Values
101.11
2
10.111
2
0.111111
2
University of Washington
Representable Values
¢
Limita.ons of frac.onal binary numbers:
§
Can only exactly represent numbers that can be wriWen as
x * 2
y
§
Other ra9onal numbers have repea9ng bit representa9ons
¢
Value
Representa.on
§
1/3
0.0101010101[01]…
2
§
1/5
0.001100110011[0011]…
2
§
1/10
0.0001100110011[0011]…
2
Frac.onal Values
University of Washington
Fixed Point Representa.on
¢
We might try represen.ng frac.onal binary numbers by
picking a fixed place for an implied binary point
§
“fixed point binary numbers”
¢
Let's do that, using 8-‐bit fixed point numbers as an example
§
#1: the binary point is between bits 2 and 3
b
7
b
6
b
5
b
4
b
3
[.] b
2
b
1
b
0
§
#2: the binary point is between bits 4 and 5
b
7
b
6
b
5
[.] b
4
b
3
b
2
b
1
b
0
¢
The posi.on of the binary point affects the range and
precision of the representa.on
§
range: difference between largest and smallest numbers possible
§
precision: smallest possible difference between any two numbers
Frac.onal Values
University of Washington
Fixed Point Pros and Cons
¢
Pros
§
It's simple. The same hardware that does integer arithme9c can do
fixed point arithme9c
§
In fact, the programmer can use ints with an implicit fixed point
§
ints are just fixed point numbers with the binary point
to the right of b
0
¢
Cons
§
There is no good way to pick where the fixed point should be
§
Some9mes you need range, some9mes you need precision – the
more you have of one, the less of the other.
Frac.onal Values