Download 1 - Wright State University

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
Chapter 2
Bits, Data Types & Operations
Bits, Data Types & Operations
z Integer Representation
z Floating‐point Representation
z Other data types
Why do Computers use Base 2?
z
Base 10 Number Representation
– Natural representation for human transactions
z
H d I l
Hard to Implement Electronically
El
i ll
3.5 = 3×100 + 5×10-1
– Hard to store
z
ENIAC (First electronic computer) used 10 vacuum tubes / digit
– Hard to transmit
z
N d hi h
Need high precision to encode 10 signal levels on single wire
ii t
d 10 i l l l
i l i
– Messy to implement digital logic functions
z
z
Addition, multiplication, etc.
Binary Representation
– Specifically
Specifically, the devices that make up a computer are switches that can be the devices that make up a computer are switches that can be
on or off, i.e. at high or low voltage.
– Easy to store with bistable elements
– Reliably transmitted on noisy and inaccurate wires z
Examples
3.5 = 1×21 + 1×20 + 1×2-11 +…
– Represent 3.510 as 11.12
– Represent 1521310 as 111011011011012
– Represent 1.2010 as 1.0011001100110011[0011]…2
z Binary floating point number cannot exactly represent $1.20
Binary floating point number cannot exactly represent $1 20
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
2
Data representation in binary
z
We need a way to represent information (data) in a form that is mutually comprehensible by human and machine.
that is mutually comprehensible by human and machine.
– We have to develop schemes for representing all conceivable types of information – integers, characters, floating point numbers, language, images, actions, etc.
z
Binary digITs (Bits): sequences of 0’s and 1’s that help humans keep track of the current flows stored/processed in the computer
– Using base‐2 provide us with two symbols to work with: we can call them on & off, or (more usefully) 0 and 1.
– We group bits together to allow representation of more complex information
l i f
ti
– For ease of use, Computer scientists usually represent quarters of bits in Hexadecimal
– eg. xA13F vs. 1010000100111111
eg xA13F vs 1010000100111111
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
Hex
Binary
x0
0000
x1
0001
x2
0010
x3
0011
x4
0100
x5
0101
x6
0110
x7
7
0111
x8
1000
x9
1001
xA
1010
xB
1011
xC
1100
xD
1101
xE
1110
CEG 320/520
Comp. Org. & Assembly
xF
1111
3
Unsigned Binary Integers
Y = “abc” = a.22 + b.21 + c.20
(where the digits a, b, c can each take on the values of 0 or 1 only)
N = number of bits
Range is: 0 ≤ i < 2N – 1
Umin = 0
Umax = 2N – 1
Problem:
• How do we represent
g
numbers?
negative
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
3-bits
5-bits
8-bits
0
000
00000
00000000
1
001
00001
00000001
2
010
00010
00000010
3
011
00011
00000011
4
100
00100
00000100
CEG 320/520
Comp. Org. & Assembly
4
Signed Magnitude
z
Leading bit is the sign bit
Y = “abc” = (-1)a (b.21 + c.20)
Range
g is:
-2N-1
+1<i <
2N-1
–1
Smin = -2N-1 + 1
Smax = 2NN-11 – 1
Problems:
• How do we do addition/subtraction?
• We have two numbers for zero (+/-)!
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
-4
10100
-3
10011
-2
10010
-1
10001
-0
10000
+00
00000
+1
00001
+2
00010
+3
00011
+4
00100
CEG 320/520
Comp. Org. & Assembly
5
Two’s Complement
-16
10000
…
…
-3
11101
-22
11110
-1
11111
0
00000
Advantages:
+1
00001
• Operations need not check the sign
+2
00010
• Only one representation for zero
+3
00011
• Efficient use of all the bits
…
…
+15
01111
z
Transformation
– To transform a into ‐a, invert all bits in a and add 1 to the result
N 1 < i < 2N-1
N1–1
R
Range
i -22N-1
is:
Tmin = -2N-1
Tmax = 2N-1 – 1
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
6
Manipulating Binary numbers ‐‐ 1
Manipulating Binary numbers z
Binary to Decimal conversion & vice‐versa
– A 4 bit binary number A = a3a2a1a0 corresponds to:
a3 x 23 + a2 x 22 + a1 x 21 + a0 x 20 = a3 x 8 + a2 x 4 + a1 x 2 + a0 x 1
(where ai = 0 or 1 only)
– A decimal number can be broken down by iteratively determining the highest power of two that “fits” in the number:
e.g. (13)10 =>
e.g. (63)10 =>
e.g. (0.63)10 =>
– In the 2’s complement representation, leading zeros do not affect the value of a positive binary number, and leading ones do not affect the value of a negative number. So:
01101 = 00001101 = 13 and 11011 = 11111011 = 5
01101 = 00001101 = 13 and 11011 = 11111011 = ‐5
00001101
11111011
00001000 => 8 (as expected!)
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
x0D
xFB
x08
CEG 320/520
Comp. Org. & Assembly
7
Manipulating Binary numbers ‐‐ 10
Manipulating Binary numbers z
Overflow
– If we add the two (2’s complement) 4 bit numbers representing 7 and 5 we get :
0111
=> +7
0101
=> +5
5
1100
=> ‐4 (in 4 bit 2’s comp.)
z
z
z
We get ‐4, not +12 as we would expect !!
We have overflowed the range of 4 bit 2
We have overflowed
the range of 4 bit 2’ss comp. (‐8 to +7), so the result is invalid.
comp ( 8 to +7) so the result is invalid
Note that if we add 16 to this result we get back 16 ‐ 4 = 12
– this is like “stepping up” to 5 bit 2’s complement representation
– In general, if the sum of two positive numbers produces a negative result, or I
l if h
f
ii
b
d
i
l
vice versa, an overflow has occurred, and the result is invalid in that representation.
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
8
CS Reality #1
z
z
The first place where the underlying hardware abstraction can’t be treated as a magic black box results in the need for data types
You’ve got to understand binary encodings.
– Can x = 20,000,000,000?
– Is x2 ≥ 0?
z
Int’s:
–
–
z
40000 * 40000 ‐‐> 1,600,000,000
50000 * 50000 ‐‐> ?? Fl t’ Yes!
Float’s: Y !
– Is (x + y) + z = x + (y + z)?
z
z
Unsigned & Signed Int’s: Float’s:
Float
s:
–
–
Yes!
(1e20 + ‐1e20) + 3.14 ‐‐> 3.14
1e20 + (‐1e20 + 3.14) ‐‐> 0? 3? 3.1?
– Is 4/5 = 4/5.0?
z
Maybe? (int) 4/5 = 0 what is 4/5.0?
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
9
C++ Integer Puzzles
– Assume machine with 32 bit word size, two’s complement integers
– For each of the following C++ expressions, either:
For each of the following C++ expressions either
z
z
Argue that is true for all argument values
Give example where not true
• x * x >= 0
• ux >= 0
Initialization
• ux > -1
1
int x = foo();
• x < 0
⇒ (2*x) < 0
int y = bar();
• x > y
⇒ -x < -y
unsigned ux = x;
• x > 0 && y > 0
⇒ x + y > 0
unsigned uy = y;
• x >= 0
⇒ -x <= 0
• x <= 0
⇒ -x >= 0
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
10
Problems
z
Patt & Patel, Chapter 2:
p
– 2.4, 2.8, 2.10, 2.11, 2.17, 2.21
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
11
Representing Strings
z
Strings in C/C++
– Represented by array of characters
– Each character encoded in ASCII format
z
z
Standard 7‐bit encoding of character set
Other encodings exist, but uncommon
– UNICODE
UNICODE 16
16‐bit
bit superset of ASCII that superset of ASCII that
includes characters for non‐English alphabets
z
Character “0” has code 0x30
– Digit i has code 0x30+i
char S[6] = "15213";
x00A6
x00A7
x00A8
x00A9
x00AA
x00AB
– String should be null‐terminated
z
x31
x35
x32
x31
x33
x00
#49
#53
#50
#49
#51
#00
Hex
Dec
Final character = 0
– Line termination, and other special characters vary
characters vary
Strings are really just arrays of numbers!
How can we represent numbers using bits?
“Hey
Hey, how do I know it
how do I know it’s a string in the first place?”
s a string in the first place?
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
12
Real numbers
z
z
Most numbers are not integer!
Range:
– The magnitude of the numbers we can represent is determined by how many bits we use:
z
z
e.g. with 32 bits the largest number we can represent is about +/‐ 2 billion, far too small for many purposes.
Precision:
– The exactness with which we can specify a number:
z
z
e.g. a 32 bit number gives us 31 bits of precision, or roughly 9 figure precision in decimal representation representation
Our decimal system handles non‐integer real numbers by adding yet another symbol ‐ the decimal point (.) to make a fixed point notation:
3 + 5.102 + 4.101 + 6.100 + 7.10‐1 + 8.10‐2
– e.g. 3,456.78 = 3.10
g ,
z
The floating point, or scientific, notation allows us to represent very large and very small numbers (integer or real), with as much or as little precision as needed.
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
13
Real numbers in a fixed space
z
z
What if you only have 8 digits to represent a real number?
Do you prefer…
... a low‐precision number with a large range?
… or a high‐precision number with a smaller range?
Wouldn’t it be nice if we could “float” the decimal f
f
point to allow for either case?
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
14
Scientific notation
z
z
z
In binary, we only have 0 and 1, how can we represent the decimal point?
i ?
In scientific notation, it’s implicit. In other words:
425000 (× 100) =
425 × 103 =
42.5 × 104 =
4.25 × 105
4.25 ×
We can represent any number with the decimal after the first digit by “floating” the decimal point to the left (or right)
0 00125 =
0.00125 =
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
15
Real numbers in binary z
We mimic the decimal floating point notation to create a “hybrid” binary floating point number:
floating point number:
– We first use a “binary point” to separate whole numbers from fractional numbers to make a fixed point notation:
z
e.g. 25.75
e.g.
25.7510 1.24 + 1.10
1.103 + 1.10
1.101 + 1.2
1.2‐1 + 1.2
1.2‐2 = 11001.1102
10 = 1.2
(2‐1 = 0.5 and 2‐2 = 0.25, etc.)
– We then “float” the binary point:
z
00011001.110 => 1.1001110 x 24
mantissa = 1.1001110, exponent = 4
– Now we have to express this without the extra symbols (
p
y
( x, 2, . )
, , )
z
by convention, we divide the available bits into three fields:
sign, mantissa, exponent
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
16
IEEE‐‐754 IEEE
754 fp
fp numbers numbers –– 32 bit “float”
32 bits:
1
8 bits
s biased exp.
23 bits
fraction
N = (-1)s x 1.fraction x 2(biased exp. – 127)
z
Sign: 1 bit
Sign: 1 bit
z
Mantissa: 23 bits
– We “normalize” the mantissa by dropping the leading 1 and recording only its f ti
fractional part (why?) l
t ( h ?)
z
Exponent: 8 bits
– In order to handle both + and ‐ exponents, we add 127 to the actual exponent to create a “biased exponent” (this provides a lexographic order!) tt
t “bi d
t” (thi
id
l
hi
d !)
z
z
z
2‐127 => biased exponent = 0000 0000 (= 0)
20 => biased exponent = 0111 1111 (= 127)
2+127 =>> biased exponent biased exponent = 1111 1110 (
1111 1110 (= 254)
254)
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
17
IEEE‐‐754 IEEE
754 fp
fp numbers numbers ‐‐ example
z
z
Example:
z
25.75 =>> 00011001.110 25.75 00011001.110 =>> 1.1001110 x 2
1.1001110 x 24
z
sign bit = 0 (+)
z
normalized mantissa (fraction) = 100 1110 0000 0000 0000 0000
z
biased exponent = 4 + 127 = 131 => 1000 0011
biased exponent = 4 + 127 = 131 => 1000 0011
z
so 25.75 => 0 1000 0011 100 1110 0000 0000 0000 0000 => x41CE0000
Special values represented by convention:
– Infinity (+ and ‐): exponent = 255 (1111 1111) and mantissa = 0
– NaN (not a number): exponent = 255 and mantissa ≠
NaN (not a number): exponent = 255 and mantissa ≠ 0
– Zero (0): exponent = 0 and mantissa = 0
z
note: special case => fraction is de‐normalized, i.e no hidden 1
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
18
IEEE‐‐754 IEEE
754 fp
fp numbers numbers –– 64
64‐‐bit “double”
Double precision (64 bit) floating point
z
64 bits:
1
11 bits
s biased exp.
52 bits
fraction
N = (-1)s x 1.fraction x 2(biased exp. – 1023)
z
R
Range
&P
Precision:
i i
Š 32 bit:
ƒ mantissa of 23 bits + 1 => approx. 7 digits decimal
+/-127
127 => approx.
+/-38
38
ƒ 2+/
approx 10+/
Š 64 bit:
ƒ mantissa of 52 bits + 1 => approx. 15 digits decimal
+/ 1023 => approx. 10+/-306
+/ 306
ƒ 2+/-1023
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
19
Floating point in C/C++
z
C Guarantees Two Levels
float
double
z
single precision
double precision
Conversions
– Casting between int, float, and double
g
,
,
changes numeric values
g
– Double or float to int
z
z
z
Truncates fractional part
Like rounding toward zero
Not defined when out of range
– Generally saturates to TMin or TMax
– int to double
z
E
Exact conversion, as long as int has ≤
i
l
i h ≤ 53 bit word size
53 bi
d i
– int to float
z
Will round according to rounding mode
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
20
Floating point puzzles in C++
int x = …;
float f = …;
;
Assume neither
d nor f is NAN
double d = …;
• x == (int)(float) x
• x == (int)(double) x
• f == (float)(double) f
• d == (float) d
• f == -(-f);
• 2/3 == 2/3.0
0.0
0 ⇒ ((d
((d*2)
2) < 0.0)
0 0)
• d < 0
• d > f
⇒ -f > -d
• d * d >= 0.0
• (d+f)-d
(d f) d == f
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
21
Another floating point puzzle
#include <iostream>
using namespace std;
int main() {
float i;
for (i=0.0;i<25.0;i+=0.1) {
cout << i << endl;
}
}
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
[...]
7
7.1
7.2
7.3
7.4
7.5
7.6
7 7
7.7
7.79999
7.89999
7.99999
8.09999
8.2
8.3
[
[...]
]
CEG 320/520
Comp. Org. & Assembly
22
Floating point number line
z
z
z
32 bits can represent 232 unique values
How are those values distributed differently between integers and y
g
floats?
What are the implications?
-1
0
+1
-10
0
+1
0
-2b
0
+2b
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
23
Ariane 5
z
z
z
Payload rocket
7 billion (Euro) in design alone
(
)
g
Danger!
– Computed horizontal velocity gp
as floating point number
– Converted to 16‐bit integer
– Worked OK for Ariane 4
– Overflowed for Ariane 5
z
z
same software
Result
– Exploded 37 seconds after xploded 37 seconds after
liftoff
– Cargo worth $500 million
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
24
Other Data Types
z
Other numeric data types
– e.g. BCD
BCD
z
Bit vectors & masks
– sometimes we want to deal with the individual bits themselves
z
Logic values
– True (non‐zero) or False (0)
z
Instructions
– Output as “data” from a compiler
z
Misc
– Grapics, sounds, …
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
25
Practice Problems
z
Patt & Patel
– 2.39, 2.40, 2.41, 2.42, 2.56
Wright State University, College of Engineering
Dr. Doom, Computer Science & Engineering
CEG 320/520
Comp. Org. & Assembly
26