chapter 2 bits, data types & operations l integer representation l floating-point representation...

26
Chapter 2 Bits, Data Types & Operations Integer Representation Floating-point Representation Other data types

Upload: darren-houston

Post on 12-Jan-2016

237 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

Chapter 2

Bits, Data Types & Operations

Integer Representation Floating-point Representation

Other data types

Page 2: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

2College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

CS Reality #1

You’ve got to understand binary encodings.

Examples– Is x2 ≥ 0?

Int’s:– 40000 * 40000 --> 1600000000– 50000 * 50000 --> ??

Float’s: Yes!– Is (x + y) + z = x + (y + z)?

Unsigned & Signed Int’s: Yes! Float’s:

– (1e20 + -1e20) + 3.14 --> 3.14– 1e20 + (-1e20 + 3.14) --> 0? 3? 3.1?

– Is 4/5 = 4/5.0? Maybe? (int) 4/5 = 0 what is 4/5.0?

Page 3: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

3College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Data types

Our first requirement is to find a way to represent information (data) in a form that is mutually comprehensible by human and machine.

– Ultimately, we will have to develop schemes for representing all conceivable types of information - language, images, actions, etc.

– Specifically, the devices that make up a computer are switches that can be on or off, i.e. at high or low voltage. Thus they naturally provide us with two symbols to work with: we can call them on & off, or (more usefully) 0 and 1.

– We will start by examining different ways of representing Strings of Characters Integers Floating point numbers

Page 4: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

4College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Why do Computers use Base 2?

Base 10 Number Representation– Natural representation for human transactions

Binary floating point number cannot exactly represent $1.20 Hard to Implement Electronically

– 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 Addition, multiplication, etc.

Base 2 Number Representation– Easy to store with bistable elements– Reliably transmitted on noisy and inaccurate wires

Examples– Represent 1521310 as 111011011011012– Represent 1.2010 as 1.0011001100110011[0011]…2– Represent 1.5213 X 104 as 1.11011011011012 X 213

1.20 = 1×20 + 0×2-1 + 0×2-2 +…

Page 5: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

5College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Unsigned Binary Integers

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

Y = “abc” = a.22 + b.21 + c.20

N = number of bitsRange is: 0 i < 2N – 1

Umin = 0Umax = 2N – 1

(where the digits a, b, c can each take on the values of 0 or 1 only)

Problem:• How do we represent

negative numbers?

Page 6: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

6College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Signed Magnitude

Leading bit is the sign bit -4 10100

-3 10011

-2 10010

-1 10001

-0 10000

+0 00000

+1 00001

+2 00010

+3 00011

+4 00100

Range is: -2N-1 + 1 < i < 2N-1 – 1

Smin = -2N-1 + 1 Smax = 2N-1 – 1

Y = “abc” = (-1)a (b.21 + c.20)

Problems:• How do we do addition/subtraction?• We have two numbers for zero (+/-)!

Page 7: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

7College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Two’s Complement

Transformation– To transform a into -a, invert all bits in a and add 1

to the result

-16 10000

… …

-3 11101

-2 11110

-1 11111

0 00000

+1 00001

+2 00010

+3 00011

… …

+15 01111

Range is: -2N-1 < i < 2N-1 – 1Tmin = -2N-1

Tmax = 2N-1 – 1

Advantages:

• Operations need not check the sign

• Only one representation for zero

• Efficient use of all the bits

Page 8: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

8College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Manipulating Binary numbers - 1

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

000011011111101100001000 => 8 (as expected!)

Page 9: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

9College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Manipulating Binary numbers - 10

Overflow– If we add the two (2’s complement) 4 bit numbers representing 7 and 5 we

get :0111 => +70101 => +51100 => -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 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 vice versa, an overflow has occurred, and the result is invalid in that representation.

Page 10: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

10College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Speeding Things up With Hex

Conversion from binary to/from Hex is much faster A 32-bit register might contain the value:

01010011101010011011101100110111 It is much faster to view it like this: x53A9BB37 What if we want to add:

01010011101010011011101100110111+ 00111100001011111010011110011100

We can do it this way: x53A9BB37+ x3C2FA79C----------- x8FD962D3

Page 11: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

11College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

char S[6] = "15213";

Representing Strings Strings in C

– Represented by array of characters– Each character encoded in ASCII format

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

– String should be null-terminated Final character = 0

– Line termination, and other special characters vary

x32x31

x31x35

x33x00

#50#49

#49#53

#51#00

HexDec

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?”

Page 12: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

12College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Interpreting data

If some memory contains x4869206D6F6D2100, is it:– Two signed or unsigned integers: x4869206D = 1,214,849,133 and

x6F6D2100 = ??– A 64-bit floating point number?– A string: “Hi mom!” ?

There is no way to know, without– Seeing the source code,– Talking to the programmer, or– Reverse engineering

Remember this: Memory and registers contain BINARY, and only binary, at all times.

Page 13: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

13College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

C Integer Puzzles

– Assume machine with 32 bit word size, two’s complement integers– 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 > -1

• x < 0 (2*x) < 0

• x > y -x < -y

• x > 0 && y > 0 x + y > 0

• x >= 0 -x <= 0

• x <= 0 -x >= 0

int x = foo();

int y = bar();

unsigned ux = x;

unsigned uy = y;

Initialization

Page 14: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

14College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Problems

Patt & Patel, Chapter 2:– 2.4, 2.8, 2.10, 2.11, 2.17, 2.21

Page 15: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

15College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Real 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 representation

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-2

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.

Page 16: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

16College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Real 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 point to allow for either case?

Page 17: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

17College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Scientific notation

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 =

Page 18: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

18College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Real numbers in binary

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: 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

Page 19: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

19College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

IEEE-754 fp numbers - 1

Sign: 1 bit Mantissa: 23 bits

– We “normalize” the mantissa by dropping the leading 1 and recording only its fractional part (why?)

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!) 2-127 => biased exponent = 0000 0000 (= 0) 20 => biased exponent = 0111 1111 (= 127) 2+127 => biased exponent = 1111 1110 (= 254)

N = (-1)s x 1.fraction x 2(biased exp. – 127)

s biased exp. fraction

1 8 bits 23 bits32 bits:

Page 20: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

20College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

IEEE-754 fp numbers - 2

Example:25.75 => 00011001.110 => 1.1001110 x 24

sign bit = 0 (+)normalized mantissa (fraction) = 100 1110 0000 0000 0000 0000biased exponent = 4 + 127 = 131 => 1000 0011so 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– Zero (0): exponent = 0 and mantissa = 0

note: special case => fraction is de-normalized, i.e no hidden 1

Page 21: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

21College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

IEEE-754 fp numbers - 3

Double precision (64 bit) floating point

s biased exp. fraction

1 11 bits 52 bits

N = (-1)s x 1.fraction x 2(biased exp. – 1023)

64 bits:

Range & Precision: 32 bit:

mantissa of 23 bits + 1 => approx. 7 digits decimal 2+/-127 => approx. 10+/-38

64 bit: mantissa of 52 bits + 1 => approx. 15 digits decimal 2+/-1023 => approx. 10+/-306

Page 22: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

22College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

C Guarantees Two Levelsfloat single precisiondouble double precision

Conversions– Casting between int, float, and double changes numeric values– Double or float to int

Truncates fractional part Like rounding toward zero Not defined when out of range

– Generally saturates to TMin or TMax

– int to double Exact conversion, as long as int has ≤ 53 bit word size

– int to float Will round according to rounding mode

Floating point in C

Page 23: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

23College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

int x = …;float f = …;double d = …;

Assume neitherd nor f is NAN

• 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 > f -f > -d• d * d >= 0.0 • (d+f)-d == f

Floating point puzzles in C

Page 24: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

24College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Practice Problems

Patt & Patel

– 2.39, 2.40, 2.41, 2.42, 2.56

Page 25: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

25College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Other Data Types

Other numeric data types– 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, …

Page 26: Chapter 2 Bits, Data Types & Operations l Integer Representation l Floating-point Representation l Other data types

26College of Charleston, School of Science and MathematicsDr. Anderson, Computer Science Department

CS 250Comp. Org. & Assembly

Ariane 5

Danger!– Computed horizontal velocity

as floating point number– Converted to 16-bit integer– Worked OK for Ariane 4– Overflowed for Ariane 5

Used same software Result

– Exploded 37 seconds after liftoff

– Cargo worth $500 million