- By
**bluma** - Follow User

- 113 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' Chapter 2' - bluma

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

### Chapter 2

Bits, Data Types & Operations

Integer Representation

Floating-point Representation

Other data types

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?

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

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+…

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 negative numbers?

Signed Magnitude

- 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 (+/-)!

Two’s Complement

- Transformation
- To transform a into -a, invert all bits in a and add 1 to the result

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

Tmin = -2N-1

Tmax = 2N-1 – 1

- Advantages:
- Operations need not check the sign
- Only one representation for zero
- Efficient use of all the bits

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

00001101

11111011

00001000 => 8 (as expected!)

Manipulating Binary numbers - 10

- 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.)

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

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

#49

x35

#53

x32

#50

x31

#49

#51

x33

x00

#00

Representing Stringschar S[6] = "15213";

- 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

Dec

Hex

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

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.

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

Initialization

int x = foo();

int y = bar();

unsigned ux = x;

unsigned uy = y;

Problems

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

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.

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?

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 =

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

32bits:

1

8 bits

s

fraction

biased exp.

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)

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 0000
- biased 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
- Zero (0): exponent = 0 and mantissa = 0
- note: special case => fraction is de-normalized, i.e no hidden 1

IEEE-754 fp numbers - 3

- Double precision (64 bit) floating point

64 bits:

52 bits

1

11 bits

s

fraction

biased exp.

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

- 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

Floating point in C

- C Guarantees Two Levels

float single precision

double 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 puzzles in C

Assume neither

d nor f is NAN

int x = …;

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

Practice Problems

- Patt & Patel
- 2.39, 2.40, 2.41, 2.42, 2.56

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, …

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

Download Presentation

Connecting to Server..