Chapter 2tdoom/courses/CEG320/notes/1up-02-representatio… · Data representation in binary zWe...
Transcript of Chapter 2tdoom/courses/CEG320/notes/1up-02-representatio… · Data representation in binary zWe...
Chapter 2Chapter 2
Bits, Data Types & OperationsBits, Data Types & Operations
Integer Representation
Floating‐point Representation
Other data types
Why do Computers use Base 2?Why do Computers use Base 2?
Base 10 Number Representation– Natural representation for human transactionsH d I l El i ll
3.5 = 3×100 + 5×10-1Hard to Implement Electronically– Hard to store
ENIAC (First electronic computer) used 10 vacuum tubes / digit– Hard to transmit
N d hi h i i t d 10 i l l l i l iNeed high precision to encode 10 signal levels on single wire– Messy to implement digital logic functions
Addition, multiplication, etc.
Binary RepresentationSpecifically the devices that make up a computer are switches that can be– Specifically, 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 Examples 1 0 1Examples– Represent 3.510 as 11.12– Represent 1521310 as 111011011011012– Represent 1.2010 as 1.0011001100110011[0011]…2
Binary floating point number cannot exactly represent $1 20
3.5 = 1×21 + 1×20 + 1×2-1 +…
2Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
Binary floating point number cannot exactly represent $1.20
Data representation in binaryData representation in binary
We need a way to represent information (data) in a form that is mutually comprehensible by human and machine.
Hex Binary
x0 0000
x1 0001that 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.
x2 0010
x3 0011
x4 0100Binary digITs (Bits): sequences of 0’s and 1’s that help humans keep track of the current flows stored/processed in the computer
x4 0100
x5 0101
x6 0110
7 0111– 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 l i f ti
x7 0111
x8 1000
x9 1001
complex information
– For ease of use, Computer scientists usually represent quarters of bits in Hexadecimal
eg xA13F vs 1010000100111111
xA 1010
xB 1011
xC 1100
3Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
– eg. xA13F vs. 1010000100111111xD 1101
xE 1110
xF 1111
Unsigned Binary IntegersUnsigned Binary Integers
Y = “abc” = a.22 + b.21 + c.20
3-bits 5-bits 8-bitsN = number of bits
(where the digits a, b, c can each take on the values of 0 or 1 only)
0 000 00000 00000000
1 001 00001 00000001
Range is: 0 ≤ i < 2N – 1
Umin = 02 010 00010 00000010
3 011 00011 00000011
Umin = 0Umax = 2N – 1
Problem:4 100 00100 00000100
Problem:• How do we represent
negative numbers?
4Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
g
Signed MagnitudeSigned Magnitude
Leading bit is the sign bit -4 10100
-3 10011
-2 10010Range is: -2N-1 + 1 < i < 2N-1 – 1
Y = “abc” = (-1)a (b.21 + c.20)
-1 10001
-0 10000
0 00000
g
Smin = -2N-1 + 1 Smax = 2N-1 1 +0 00000
+1 00001
+2 00010
Smax = 2N 1 – 1
Problems:+2 00010
+3 00011
+4 00100
• How do we do addition/subtraction?• We have two numbers for zero (+/-)!
5Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
+4 00100
Two’s ComplementTwo’s Complement
Transformation -16 10000
– To transform a into ‐a, invert all bits in a and add 1 to the result
… …
-3 11101
2 11110R i 2N 1 < i < 2N 1 1 -2 11110
-1 11111
0 00000
Range is: -2N-1 < i < 2N-1 – 1Tmin = -2N-1
Tmax = 2N-1 – 1+1 00001
+2 00010
Advantages:• Operations need not check the sign
+3 00011
… …
+15 01111
• Only one representation for zero• Efficient use of all the bits
6Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
+15 01111
Manipulating Binary numbers Manipulating Binary numbers ‐‐ 11
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 = 501101 = 00001101 = 13 and 11011 = 11111011 = ‐5
00001101 x0D11111011 xFB
7Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
00001000 => 8 (as expected!) x08
Manipulating Binary numbers Manipulating Binary numbers ‐‐ 1010
Overflow– If we add the two (2’s complement) 4 bit numbers representing 7 and 5 we get :
0111 => +7
0101 50101 => +5
1100 => ‐4 (in 4 bit 2’s comp.)We get ‐4, not +12 as we would expect !!
We have overflowed the range of 4 bit 2’s comp ( 8 to +7) so the result is invalidWe have overflowed the range of 4 bit 2 s 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
I l if h f i i b d i l– In general, if the sum of two positive numbers produces a negative result, or vice versa, an overflow has occurred, and the result is invalid in that representation.
8Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
CS Reality #1CS Reality #1
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?Int’s:
– 40000 * 40000 ‐‐> 1,600,000,000
– 50000 * 50000 ‐‐> ??
Fl t’ Y !Float’s: Yes!
– Is (x + y) + z = x + (y + z)?Unsigned & Signed Int’s: Yes!
Float’s:Float s:– (1e20 + ‐1e20) + 3.14 ‐‐> 3.14
– 1e20 + (‐1e20 + 3.14) ‐‐> 0? 3? 3.1?
– Is 4/5 = 4/5.0?
9Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
Maybe? (int) 4/5 = 0 what is 4/5.0?
C++ Integer PuzzlesC++ 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:Argue that is true for all argument values
Give example where not true
• x * x >= 0
• ux >= 0
• ux > -1Initialization ux > 1
• x < 0 ⇒ (2*x) < 0
• x > y ⇒ -x < -y
int x = foo();
int y = bar();
Initialization
• x > 0 && y > 0 ⇒ x + y > 0
• x >= 0 ⇒ -x <= 0
y
unsigned ux = x;
unsigned uy = y;
10Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
• x <= 0 ⇒ -x >= 0
ProblemsProblems
Patt & Patel, Chapter 2:p– 2.4, 2.8, 2.10, 2.11, 2.17, 2.21
11Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
Representing StringsRepresenting Strings
char S[6] = "15213";Strings in C/C++– Represented by array of characters– Each character encoded in ASCII format
Standard 7‐bit encoding of character setOther encodings exist, but uncommon
– UNICODE 16‐bit superset of ASCII that x32
x31x35
#50
#49#53
x00A8
x00A6x00A7
UNICODE 16 bit superset of ASCII that includes characters for non‐English alphabets
Character “0” has code 0x30– Digit i has code 0x30+i
x31x33x00
#49#51#00
x00A9x00AAx00AB
– String should be null‐terminatedFinal character = 0
– Line termination, and other special characters vary
Hex Dec
characters vary
Strings are really just arrays of numbers!Strings are really just arrays of numbers!How can we represent numbers using bits?How can we represent numbers using bits?
“Hey how do I know it’s a string in the first place?”“Hey how do I know it’s a string in the first place?”
12Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
Hey, how do I know it s a string in the first place?Hey, how do I know it s a string in the first place?
Real numbersReal numbers
Most numbers are not integer!
Range:– The magnitude of the numbers we can represent is determined by how many bits we use:
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:
e.g. a 32 bit number gives us 31 bits of precision, or roughly 9 figure precision in decimal representationrepresentation
Our decimal system handles non‐integer real numbers by adding yet another symbol ‐ the decimal point (.) to make a fixed point notation:– e.g. 3,456.78 = 3.103 + 5.102 + 4.101 + 6.100 + 7.10‐1 + 8.10‐2g ,
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.
13Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
needed.
Real numbers in a fixed spaceReal numbers in a fixed space
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 Wouldn’t it be nice if we could “float” the decimal f ff fpoint to allow for either case?point to allow for either case?
14Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
Scientific notationScientific notation
In binary, we only have 0 and 1, how can we represent the decimal i ?point?
In scientific notation, it’s implicit. In other words:
425000 (× 100) =
425 × 103 =
42.5 × 104 =
4.25 × 1054.25 × 10
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 =
15Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
Real numbers in binary Real numbers in binary
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:
e.g. 25.7510 = 1.24 + 1.103 + 1.101 + 1.2‐1 + 1.2‐2 = 11001.1102e.g. 25.7510 1.2 1.10 1.10 1.2 1.2 11001.1102(2‐1 = 0.5 and 2‐2 = 0.25, etc.)
– We then “float” the binary point:00011001.110 => 1.1001110 x 24
mantissa = 1.1001110, exponent = 4
– Now we have to express this without the extra symbols ( x, 2, . )p y ( , , )by convention, we divide the available bits into three fields:
sign, mantissa, exponent
16Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
IEEEIEEE‐‐754 754 fpfp numbers numbers –– 32 bit “float”32 bit “float”
s biased exp. fraction1 8 bits 23 bits32 bits:
Sign: 1 bitN = (-1)s x 1.fraction x 2(biased exp. – 127)
s biased exp. fraction
Sign: 1 bit
Mantissa: 23 bits– We “normalize” the mantissa by dropping the leading 1 and recording only its
f ti l t ( h ?)fractional part (why?)
Exponent: 8 bits– In order to handle both + and ‐ exponents, we add 127 to the actual
t t t “bi d t” (thi id l hi d !)exponent to create a “biased exponent” (this provides a lexographic order!) 2‐127 => biased exponent = 0000 0000 (= 0)
20 => biased exponent = 0111 1111 (= 127)
2+127 => biased exponent = 1111 1110 (= 254)
17Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
2 > biased exponent 1111 1110 ( 254)
IEEEIEEE‐‐754 754 fpfp numbers numbers ‐‐ exampleexample
Example:25.75 => 00011001.110 => 1.1001110 x 2425.75 > 00011001.110 > 1.1001110 x 2
sign bit = 0 (+)
normalized mantissa (fraction) = 100 1110 0000 0000 0000 0000
biased exponent = 4 + 127 = 131 => 1000 0011biased exponent = 4 + 127 = 131 => 1000 0011
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 ≠ 0– NaN (not a number): exponent = 255 and mantissa ≠ 0
– Zero (0): exponent = 0 and mantissa = 0
note: special case => fraction is de‐normalized, i.e no hidden 1
18Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
IEEEIEEE‐‐754 754 fpfp numbers numbers –– 6464‐‐bit “double”bit “double”
Double precision (64 bit) floating point
s biased exp. fraction1 11 bits 52 bits64 bits:
N = (-1)s x 1.fraction x 2(biased exp. – 1023)
R & P i iRange & Precision:32 bit:
mantissa of 23 bits + 1 => approx. 7 digits decimal2+/-127 => approx 10+/-382+/ 127 => approx. 10+/ 38
64 bit: mantissa of 52 bits + 1 => approx. 15 digits decimal2+/ 1023 10+/ 306
19Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
2+/-1023 => approx. 10+/-306
Floating point in C/C++Floating point in C/C++
C Guarantees Two Levelsfloat single precision
double double precision
Conversions– Casting between int, float, and double changes numeric valuesg , , g
– Double or float to intTruncates fractional part
Like rounding toward zero
Not defined when out of range– Generally saturates to TMin or TMax
– int to doubleE i l i h ≤ 53 bi d iExact conversion, as long as int has ≤ 53 bit word size
– int to floatWill round according to rounding mode
20Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
Floating point puzzles in C++Floating point puzzles in C++
int x = …;
float f = ;
Assume neitherd nor f is NANfloat f = …;
double d = …;
• x == (int)(float) x
• x == (int)(double) x
• f == (float)(double) f
• d == (float) d
• f == -(-f);
• 2/3 == 2/3.0
• d < 0 0 ⇒ ((d*2) < 0 0)d < 0.0 ⇒ ((d 2) < 0.0)
• d > f ⇒ -f > -d
• d * d >= 0.0
(d f) d f
21Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
• (d+f)-d == f
Another floating point puzzleAnother floating point puzzle
#include <iostream>
using namespace std;[...]
7int main() {
float i;
for (i=0.0;i<25.0;i+=0.1) {
cout << i << endl;
7.1
7.2
7.3cout << i << endl;
}
}
7.4
7.5
7.6
7 77.7
7.79999
7.89999
7.99999
8.09999
8.2
8.3
[ ]
22Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
[...]
Floating point number lineFloating point number line
32 bits can represent 232 unique values
How are those values distributed differently between integers and y gfloats?
What are the implications?
0-1 +1
0-10 +10
0-2b +2b
23Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
Ariane 5Ariane 5
Payload rocket
7 billion (Euro) in design alone( ) g
Danger!– Computed horizontal velocity
as floating point numberg p
– Converted to 16‐bit integer
– Worked OK for Ariane 4
– Overflowed for Ariane 5same software
Result– Exploded 37 seconds after xploded 37 seconds after
liftoff
– Cargo worth $500 million
24Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly
Other Data TypesOther Data Types
Other numeric data typesBCD– e.g. BCD
Bit vectors & masks– sometimes we want to deal with the individual bits themselves
Logic values– True (non‐zero) or False (0)
Instructions– Output as “data” from a compiler
Misc– Grapics, sounds, …
25Wright State University, College of EngineeringDr. Doom, Computer Science & Engineering
CEG 320/520Comp. Org. & Assembly