Download Why do Computers use Base 2?

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
Why do Computers use Base 2?
Chapter 2

Base 10 Number Representation

Hard to Implement Electronically
– Natural representation for human transactions
3.5 = 3×100 + 5×10-1
– Hard to store

ENIAC (First electronic computer) used 10 vacuum tubes / digit
– Hard to transmit

Need high precision to encode 10 signal levels on single wire
– Messy to implement digital logic functions


Bits, Data Types & Operations
– 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
 Integer Representation
 Floating-point Representation
 Other data
Addition, multiplication, etc.
Binary Representation

types
Examples
3.5 = 1×21 + 1×20 + 1×2-1 +…
– 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
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
Data representation in binary

We need a way to represent information (data) in a form
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.

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
– For ease of use, Computer scientists usually represent
quarters of bits in Hexadecimal
– eg. xA13F vs. 1010000100111111
Binary
x0
0000
x1
0001
x2
0010
x3
0011
x4
0100
x5
0101
x6
0110
x7
0111
x8
1000
x9
1001
xA
1010
xB
1011
xC
1100
xD
1101
xF
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
negative numbers?
Leading bit is the sign bit
Y = “abc” = (-1)a (b.21 + c.20)
Range is: -2N-1 + 1 < i < 2N-1 – 1
Smin = -2N-1 + 1
Smax = 2N-1 – 1
Problems:
• How do we do addition/subtraction?
• We have two numbers for zero (+/-)!
Wr ight State Univer sity, 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
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
3
CEG 320/520
Comp. Or g. & Assemb ly
4
1111
Signed Magnitude

2
Unsigned Binary Integers
Hex
xE
1110
CEG 320/520
Comp. Or g. & Assemb ly
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
CEG 320/520
Comp. Or g. & Assemb ly
Two’s Complement
-4
10100
-3
10011
-2
10010
-1
10001
-0
10000
+0
00000
+1
00001
+2
00010
+3
00011
+4
00100
CEG 320/520
Comp. Or g. & Assemb ly

5
Transformation
-16
10000
– To transform a into -a, invert all bits in a and add 1
to the result
…
…
-3
11101
Range is: -2N-1 < i < 2N-1 – 1
Tmin = -2N-1
Tmax = 2N-1 – 1
-2
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
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
CEG 320/520
Comp. Or g. & Assemb ly
6
1
Manipulating Binary numbers - 10
Manipulating Binary numbers - 1

Binary to Decimal conversion & vice-versa

– A 4 bit binary number A = a3a2a1a0 corresponds to:
Overflow
– If we add the two (2’s complement) 4 bit numbers representing 7 and 5 we
get :
0111
=> +7
0101
=> +5
1100
=> -4 (in 4 bit 2’s comp.)
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:
– this is like “stepping up” to 5 bit 2’s complement representation
01101 = 00001101 = 13 and 11011 = 11111011 = -5
00001101
11111011
00001000 => 8 (as expected!)
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
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 invalid.
Note that if we add 16 to this result we get back 16 - 4 = 12
– 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.
x0D
xFB
x08
CEG 320/520
Comp. Or g. & Assemb ly
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
7
CS Reality #1


– Assume machine with 32 bit word size, two’s complement integers
– For each of the following C++ expressions, either:

– Can x = 20,000,000,000?
– Is x2 ≥ 0?

• x * x >= 0
• ux >= 0
Initialization
Float’s: Yes!
– Is (x + y) + z = x + (y + z)?


Unsigned & Signed Int’s:
Float’s:
– Is 4/5 = 4/5.0?
• 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
Maybe? (int) 4/5 = 0 what is 4/5.0?
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
CEG 320/520
Comp. Or g. & Assemb ly
 -x >= 0
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
9
Problems
CEG 320/520
Comp. Or g. & Assemb ly
10
Representing Strings


• ux > -1
int x = foo();
Yes!
– (1e20 + -1e20) + 3.14 --> 3.14
– 1e20 + (-1e20 + 3.14) --> 0? 3? 3.1?

Argue that is true for all argument values
Give example where not true
Int’s:
– 40000 * 40000 --> 1,600,000,000
– 50000 * 50000 --> ??

8
C++ Integer Puzzles
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.

CEG 320/520
Comp. Or g. & Assemb ly
Patt & Patel, Chapter 2:
Strings in C/C++
– Represented by array of characters
– Each character encoded in ASCII format
– 2.4, 2.8, 2.10, 2.11, 2.17, 2.21


Standard 7-bit encoding of character set
Other encodings exist, but uncommon
– UNICODE 16-bit superset of ASCII that
includes
characters for non-English alphabets

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

x31
x35
x32
x31
x33
x00
#49
#53
#50
#49
#51
#00
Hex
Dec
Final character = 0
– Line termination, and other special
characters vary
Strings are really just arrays of numbers!
How can we represent numbers using bits?
“Hey, how do I know it’s a string in the first place?”
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
CEG 320/520
Comp. Or g. & Assemb ly
11
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
CEG 320/520
Comp. Or g. & Assemb ly
12
2
Real numbers


Real numbers in a fixed space
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:
… or a high-precision number with a smaller range?
– The exactness with which we can specify a number:

What if you only have 8 digits to represent a real number?
Do you prefer…
... a low-precision number with a large range?
e.g. a 32 bit number gives us 31 bits of precision, or roughly 9 figure precision in decimal
representation

Our decimal system handles non-integer real numbers by adding yet another
symbol - the decimal point (.) to make a fixed point notation:

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.
Wouldn’t it be nice if we could “float” the decimal
point to allow for either case?
– e.g. 3,456.78 = 3.103 + 5.102 + 4.101 + 6.100 + 7.10-1 + 8.10-2
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
CEG 320/520
Comp. Or g. & Assemb ly
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
13
Scientific notation




We mimic the decimal floating point notation to create a “hybrid” binary
floating point number:
– We first use a “binary point” to separate whole numbers from fractional
numbers to make a fixed point notation:

CEG 320/520
Comp. Or g. & Assemb ly
e.g. 25.7510 = 1.24 + 1.103 + 1.101 + 1.2-1 + 1.2-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, . )

by convention, we divide the available bits into three fields:
sign, mantissa, exponent
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
15
IEEE--754 fp numbers – 32 bit “float”
IEEE
32 bits:
8 bits
23 bits
s biased exp.
fraction
1
CEG 320/520
Comp. Or g. & Assemb ly

N = (-1)s x 1.fraction x 2(biased exp. – 127)
Example:

25.75 => 00011001.110 => 1.1001110 x 24

sign bit = 0 (+)

normalized mantissa (fraction) = 100 1110 0000 0000 0000 0000
Sign: 1 bit

biased exponent = 4 + 127 = 131 => 1000 0011

Mantissa: 23 bits

so 25.75 => 0 1000 0011 100 1110 0000 0000 0000 0000 => x41CE0000
– We “normalize” the mantissa by dropping the leading 1 and recording only its
fractional part (why?)

Exponent: 8 bits


– NaN (not a number): exponent = 255 and mantissa  0
2-127 => biased exponent = 0000 0000 (= 0)
20 => biased exponent = 0111 1111 (= 127)
2+127 => biased exponent = 1111 1110 (= 254)
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
CEG 320/520
Comp. Or g. & Assemb ly
Special values represented by convention:
– Infinity (+ and -): exponent = 255 (1111 1111) and mantissa = 0
– In order to handle both + and - exponents, we add 127 to the actual
exponent to create a “biased exponent” (this provides a lexographic order!)

16
IEEE--754 fp numbers - example
IEEE


14
Real numbers in binary
In binary, we only have 0 and 1, how can we represent the decimal
point?
In scientific notation, it’s implicit. In other words:
425000 (× 100) =
425 × 103 =
42.5 × 104 =
4.25 × 105
We can represent any number with the decimal after the first digit by
“floating” the decimal point to the left (or right)
0.00125 =
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
CEG 320/520
Comp. Or g. & Assemb ly
– Zero (0): exponent = 0 and mantissa = 0

17
note: special case => fraction is de-normalized, i.e no hidden 1
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
CEG 320/520
Comp. Or g. & Assemb ly
18
3
IEEE--754 fp numbers – 64
IEEE
64--bit “double”
Floating point in C/C++

Double precision (64 bit) floating point

64 bits:
1
s biased exp.
N=
(-1)s
fraction
x 1.fraction x

Conversions
– Casting between int, float, and double changes numeric values
– Double or float to int
2(biased exp. – 1023)


single precision
double precision
float
double
52 bits
11 bits
C Guarantees Two Levels

Range & Precision:

 32 bit:
Truncates fractional part
Like rounding toward zero
Not defined when out of range
– Generally saturates to TMin or TMax
 mantissa of 23 bits + 1 => approx. 7 digits decimal
 2+/-127 => approx. 10+/-38
– int to double

Exact conversion, as long as int has ≤ 53 bit word size
– int to float
 64 bit:
 mantissa of 52 bits + 1 => approx. 15 digits decimal
 2+/-1023 => approx. 10+/-306
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering

CEG 320/520
Comp. Or g. & Assemb ly
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
19
Floating point puzzles in C++
float f = …;
#include <iostream>
using namespace std;
int main() {
float i;
for (i=0.0;i<25.0;i+=0.1) {
cout << i << endl;
}
}
double d = …;
• x == (int)(float) x
• x == (int)(double) x
• f == (float)(double) f
• f == -(-f);
• 2/3 == 2/3.0
• d < 0.0 ((d*2) < 0.0)
-f > -d
• d * d >= 0.0
• (d+f)-d == f
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
CEG 320/520
Comp. Or g. & Assemb ly
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
21
Floating point number line



0
+1
-10
0
+1
0
-2b
0
+2b



22
CEG 320/520
Comp. Or g. & Assemb ly
24
Payload rocket
7 billion (Euro) in design alone
Danger!
– Computed horizontal velocity
as floating point number
– Converted to 16-bit integer
– Worked OK for Ariane 4
– Overflowed for Ariane 5
same software


Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
CEG 320/520
Comp. Or g. & Assemb ly
Ariane 5
32 bits can represent 232 unique values
How are those values distributed differently between integers and
floats?
What are the implications?
-1
20
[...]
7
7.1
7.2
7.3
7.4
7.5
7.6
7.7
7.79999
7.89999
7.99999
8.09999
8.2
8.3
[...]
• d == (float) d
• d > f
CEG 320/520
Comp. Or g. & Assemb ly
Another floating point puzzle
Assume neither
d nor f is NAN
int x = …;
Will round according to rounding mode
Result
– Exploded 37 seconds after
liftoff
– Cargo worth $500 million
CEG 320/520
Comp. Or g. & Assemb ly
23
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
4
Other Data Types

Other numeric data types

Bit vectors & masks
Practice Problems

– e.g. BCD
Patt & Patel
– 2.39, 2.40, 2.41, 2.42, 2.56
– sometimes we want to deal with the individual bits themselves

Logic values

Instructions

Misc
– True (non-zero) or False (0)
– Output as “data” from a compiler
– Grapics, sounds, …
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
CEG 320/520
Comp. Or g. & Assemb ly
25
Wr ight State Univer sity, College of Engineering
Dr . Doom, Computer Science & Engineering
CEG 320/520
Comp. Or g. & Assemb ly
26
5